An analysis of the situation showed only two ways out of the situation:
- Creating a database for testing and conducting all manipulations on it. This approach is almost immediately thrown back for personal reasons - the tests are dependent on each other.
Creating a wrapper on the context and further use through the wrapper interface:
interface IDbRepository { DbContext Context { get; } IQueryable<MyType> MyTypes { get; } // etc. }
And the wrapper itself
public class DbRepository : IDbRepository { DbContext context; public DbRepository(DbContext context) { this.context = context; } public DbContext Context { get { return context; } } public IQueryable<MyType> MyTypes { get { return context.Set<MyType>(); } }
We replace the old context with the interface
public UserRepository (DbContext context)
public UserRepository(IDbRepository repository)
And now you can test the UserRepository using Mock
Mock<IDbRepository> wrapper = new Mock<IDbRepository>(); wrapper.Setup(c => c.MyTypes).Returns( new [] { new MyType(...) }.AsQueryable() );
I doubt the correctness. Repositories are now less flexible.