These are chat archives for Behat/Behat

9th
Mar 2015
Dušan Kasan
@DusanKasan
Mar 09 2015 10:07

I am starting using Behat and I was thinking what is the correct way to do something like this:

I am testing system behaviour with Behat (the domain, not web) and i want to test behaviour that uses UserService class. UserService takes UserRepository (Interface) as an argument. Should i create the UserService object in the context or should i take it from some dependency injection container (which i currently don't have since i want to model the domain first)? Or should i maybe create a factory to construct the UserService? Should I mock those dependencies?

The problem here is, that there may be multiple implementations of UserRepository and i want to test the one the system uses (hence taking UserService from DI container). I do not know which implementation i am going to use at the time of writing the feature file/context. Probably just some dummy FilesystemUserRepository just to pass the tests.

If I used the FilesystemUserRepository in contexts, then after i decide to move to DatabaseUserRepository later in development i would have to rewrite all the contexts that use UserService class.

Is there some best practice how to do this?

Dušan Kasan
@DusanKasan
Mar 09 2015 11:06
Konstantin Kudryashov
@everzet
Mar 09 2015 12:31
I'd use InMemory versions of repositories and instantiate object graph directly in my context. Doing otherwise will hide complexity of your model behind DIC. "Shit, I have too many dependencies for this object" is very important design feedback that you hide by using DIC.
Dušan Kasan
@DusanKasan
Mar 09 2015 13:17

@everzet that was my thinking initially, but wouldn't this require me to change all contexts (the part where i'll resolve the deps graph) when i settle for final implementation of the repository in question. However, at this point i will most likely have chosen a DIC.

So maybe in early stages i should use in-memory repositories and resolve dependencies manually and after i settle for a specific DIC I should use Behat extension for that specific DIC to pass dependencies into contexts... Decisions, decisions...

Christophe Coevoet
@stof
Mar 09 2015 14:56
@DusanKasan your object model should not care whether the repository used is in memory or in DB. So I would keep using the inMemory repository in the context, making your scenario very fast.
And then you should of course have integration tests for your DB repository to ensure its behavior
Konstantin Kudryashov
@everzet
Mar 09 2015 23:09
What @stof said