tags:

views:

52

answers:

1

Let say I got the following code in my SqlUserRepository :

var user = from u in NHibernateLinqContext.Linq<User>() ...

What I would like is to have a similar context for my FakeuserRepository

var user = from u in FakeLinqContext.Linq<User>() ...

Like that, I'll be able to use the same logic in my SqlUserRespository that my FakeUserRepository. But, I don't know how Linq context work and how I can be able to create one for my FakeUserRepository.

Ideally I would like that, by example, my AnythingUserRepository take a context in constructor parameter.

Example :

SqlUserRepository() : base(NHibernateLinqcontext)
FakeUserRepository() : base(FakeLinqcontext)

Any idea how I can achieve that ?

+1  A: 

You could allow the NHibernateLinqContext dependency to be injected into your SqlUserRepository by giving it an appropriate constructor, e.g.

public SqlUserRepository(NHibernateLinqContext context)
{
    // ...
}

If you want to unit test your SqlUserRepository then you could supply a mocked NHibernateLinqContext to the construction of your target.

If you want to unit test a class which uses your SqlUserRepository then you could simply mock the SqlUserRepository. I'm not sure why you would need a concrete FakeUserRepository.

AdamRalph
How can I mock my SqlUserRepository if my SqlUserRepository is doing database access ?
Melursus
The whole point of mocking an object is to remove it as a dependency for a unit test to pass. if your unit test is actually accessing a database then it is not a unit test. The idea is that you would provide a mocked SqlUserRepository which provides canned answers to calls which the test target would make.
AdamRalph