0

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?

4
  • Can you elaborate on your first issue a bit more - finding it difficult to contextualise.
    – Ahmad
    Oct 22, 2010 at 13:50
  • @Ahmad: Entities with relationships (i.e. an author has books, a book has authors and publishers, etc.). Creating a list of authors, a list of books, etc. is pretty easy, but establishing the relationships between them (i.e. by calling add methods on the entities in the repository constructors) gets a bit messy.
    – Mayo
    Oct 22, 2010 at 14:15
  • Entities with relationships are common, that's what would form your domain model. Now I don't want to confuse things here, but it seems that what missing here is a further understanding of repository design. Within DDD (Domain Driven Design), each aggregate has a repository - which kinda relates back to the relationships between entities
    – Ahmad
    Oct 22, 2010 at 15:48
  • On a unrelated note, most examples apps use the service layer, repository layer, with an anemic domain model (classes with getters and setters). IMO thats become the mindset when creating asp-mvc apps - the problem with this I think is when you actually try to model and design something a bit more complex it falls over
    – Ahmad
    Oct 22, 2010 at 15:48

2 Answers 2

3

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:

1

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

1
  • Thanks Ahmad - I didn't even know the TearDown attribute existed - I'll check on that.
    – Mayo
    Oct 22, 2010 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.