I have several controllers that I'm testing, each of which has a repository dependency. This is how I deliver the mock repository for each test fixture:
[SetUp]
public void SetUp()
{
var repository = RepositoryMockHelper.MockRepository();
controller = new HomeController(repository.Object);
}
And here is a helper method MockRepositoryfor a good measure:
internal static Mock<IRepository> MockRepository()
{
var repository = new Mock<IRepository>();
var posts = new InMemoryDbSet<Post>()
{
new Post {
...
},
...
};
repository.Setup(db => db.Posts).Returns(posts);
return repository;
}
... = code removed for the sake of brevity.
I intend to use a new instance InMemoryDbSetfor each test. I thought using an attribute SetUpwould achieve this, but obviously would not.
When I run all the tests, the results are incompatible because the tests do not seem to be isolated. One test, for example, will remove an item from the data store and claim that the counter has been lowered, but according to the whim of the test runner, another test may increase the count, which will cause both tests to fail.
? ?