Using Moq with NUnit
Published 19 Aug 2009
As I continue to work on the NerdDinner tutorial, I have discovered that some of my development patterns has to change. Surprise? One thing that has to change is how I write tests with NUnit, when testing my repositories.
I use to create test classes that have private objects that are initialized in a
TextFixtureSetUp method. If we consider that I am to test the DinnerController,
using a mock repository, the approach would be as such:
However, consider the following tests:
Since the two test cases use the same object instances, their verifications will not work as expected. The repository’s Get function will be called twice, since both test cases call it.
The solution is (thanks, Micke!) to replace
The first is called once, prior to all tests, which the second is called before
each test case.