views:

26

answers:

2

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 fake 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:

  1. How do you go about setting up a fake collection for an entity that references other fake collections/entities?

  2. 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?

+1  A: 

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).

Ahmad
Thanks Ahmad - I didn't even know the TearDown attribute existed - I'll check on that.
Mayo
+2  A: 

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:

Darin Dimitrov