Mayo
Mayo

Reputation: 10802

Unit Testing - Challenges with Dynamic / Interrelated Repositories

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:

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?

Upvotes: 0

Views: 224

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

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:

Upvotes: 3

Ahmad
Ahmad

Reputation: 24877

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

Upvotes: 1

Related Questions