bitbonk
bitbonk

Reputation: 49629

unit test to verify that a repository loads an entity from the database correctly

I have a simple standard repository that loads a composite entity from the database. It injects all dependencies it need to read the complete entity tree from a database through IDbConnection (wich gives the repository access to IDbCommand, IDbTransaction, IDataReader) that I could mock.

public class SomeCompositionRootEntityRepository : 
    IRepository<SomeCompositionRoot>
{
    public RecipeRepository(IDbConnection connection) { ... }
    public void Add(SomeCompositionRootEntity item) { ... }
    public bool Remove(SomeCompositionRootEntity item) { ... }
    public void Update(SomeCompositionRootEntity item) { ... }
    public SomeCompositionRootEntity GetById(object id) { ... }
    public bool Contains(object id) { ... }
}

The question is how would I write unit test for this in a good way? If I want to test that the reposity has read the whole tree of objects and it has read it correcty, I would need to write a hughe mock that records and verifies the read of each and every property of each and every object in the tree. Is this really the way to go?

Update: I think I need to refactor my repository to break the repository functionality and unit test into smaller units. How could this be done?

I am sure I do not want to write unit test that involve reading and writing from and to an actual database.

Upvotes: 1

Views: 152

Answers (2)

Arne Deutsch
Arne Deutsch

Reputation: 14769

The question is: What functionality do you want to test?

  1. Do you want to test that your repository actually loads something? In this case I would write a few (!) tests that go through the database.
  2. Or do you want to test the functionality inside the repository methods? In this case your mock approach would be appropriate.
  3. Or do you want to make sure that the (trivial) methods of your repository aren't broken? In this case mocks and one test per method might be sufficient.

Just ask yourself what the tests shall ensure and design them along this target!

Upvotes: 1

jeriley
jeriley

Reputation: 1333

I think I understand your question so correct me if I'm wrong...

This is a where you cross the line from unit to integration. The test makes sense (I've written these myself), but you're not really testing your repository, rather you're testing that your entity (SomeCompositionRoot) maps correctly. It isn't inserting/updating, but does involve a read to the database.

Depending on what ORM you use, you can do this a million different ways and typically its fairly easy.

::EDIT:: like this for linq for example ...

    [TestMethod]
    public void ShouldMapBlahBlahCorrectly()
    {
        CheckBasicMapping<BlahBlah>();
    }

    private T CheckBasicMapping<T>() where T : class
    {
        var target = _testContext.GetTable<T>().FirstOrDefault();
        target.ShouldNotBeNull();
        return target;
    }

Upvotes: 0

Related Questions