I have a handful of controllers and each controller has a test class with unit tests. Each unit test calls a single action and verifies that the action handles a given scenario.
The test class has a setup routine that instantiates a number of fake repositories and other fake objects. The fak开发者_Go百科e repositories have static collections that the repository methods/functions operate against.
It is working pretty well but I'm running into some challenges:
When entities in one fake collection reference entities in another fake collection the code in the repository's constructor explodes and becomes difficult to manage
When a unit test calls an action that modifies the fake repository data, the static collection changes state making it nearly impossible to operate against that same data in other unit tests
So I have two questions that might require you to also explain your general approach:
How do you go about setting up a fake collection for an entity that references other fake collections/entities?
Do your fake repositories support update/insert/delete operations? If so, how do you prevent changes from one unit test from impacting another unit test?
As long as your repositories are abstracted with interfaces you could use a mock object framework to generate a fake repository and inject it into the controller being tested. Here are some popular choices:
- Rhino Mocks
- NSubstitute
- Moq
With regards to point 2, why not setup the fake repository in the Setup
function on your unit test class (which you do) and use the TearDown
to reset the state of the repository after each test.
(These are NUnit specific attributes, so I can't comment if other frameworks have the similar features).
精彩评论