Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Properly Unit-Test CRUD Operations on a Repository?

Realize this may sound like a broad question - so let me clarify. I have a Repository exposed via an interface, with two concrete implementations - a MockRepository and a EntityFrameworkRepository.

Now, i have a Unit-Test project for which all tests can be run against either repository, via flicking over a line in [TestInitialize].

My question is basically "How should i write the tests".

Here's what i have:

C reate

// Arrange.
var foo = new Foo { .. };

// Act
Repository.Add(foo);
UnitOfWork.Commit();

// Assert
Assert.IsTrue(foo.Id > 0);

R etrieve

// Arrange.
var fooToGet = 1;

// Act
var foo = Repository.FindSingle(fooToGet);

// Assert
Assert.IsNotNull(foo);
Assert.AreEqual(foo.Id, fooToGet);

U pdate

// Arrange.
var fooToGet = 1;
var nameToChangeFooTo = "FooBar";

// Act
var foo = Repository.FindSingle(fooToGet);
foo.Name = nameToChangeFooTo;
UnitOfWork.Commit();
var fooRetrievedAgain = Repository.FindSingle(fooToGet);

// Assert
Assert.IsNotNull(foo);
Assert.AreEqual(fooRetrievedAgain.Id, fooToGet);
Assert.AreEqual(fooRetrievedAgain.Name, nameToChangeFooTo);

D elete

// Arrange.
var fooToGet = 1;

// Act
var foo = Repository.FindSingle(fooToGet);
Repository.Remove(foo);
UnitOfWork.Commit();
var fooRetrievedAgain = Repository.FindSingle(fooToGet);

// Assert
Assert.IsNull(fooRetrievedAgain);

It's working ok, for both the Mock and EF repository, but my main problem is C (Create). I'm not sure how to test an Add operation on my Repository. It doesn't feel right what im doing.

It passes for the EF Repository, but to make it pass in my Mock Repository i had to use reflection to update the ID in the in-memory collection (nasty).

So - can you please share some advice on what are the correct ways to test CRUD operations on the Repository Pattern?

This is an ASP.NET MVC Application, .NET 4, C#, Entity Framework 4 and Unit of Work/Repository Patterns.

Thanks.

EDIT

Just to clarify guys, these are not all the unit-tests i have. I have unit tests for my service layer, as well as business-rule tests.

Both the latter will (and should) fail if my above Repository tests fail. That is the point here, to unit-test the very basic operations of my Repositories. Am i wrong?

like image 857
RPM1984 Avatar asked Oct 22 '10 03:10

RPM1984


People also ask

Should you unit test CRUD operations?

If all your application does is CRUD, then there is no point in unit testing it. Now, if there is any kind of business logic manipulating the values as they come out of the db or validating them before them going in, yes, it is a good idea to build unit tests. Testing the CRUD part does not belong in unit testing IMO.

Do we write test cases for repository?

In a modern test management tool, a test repository crops up as you write test cases and keep associating with requirements. It is a live working document that is embedded in your project in a granular way. QA teams can write tests once and easily access them for future use.

How do you write a JUnit test case for a repository?

You can create a @DataJpaTest and @Autowire your repository into it. For example: @RunWith(SpringRunner. class) @DataJpaTest public class MyJpaTest { @Autowired private ChartRepository chartRepository; @Test public void myTest() { ... } }


2 Answers

One option is to use an in-memory DB like SqlLite to test the behaviour of your mappings, queries and repositories. This is discussed by Ayende here, though his example uses NHibernate.

Another option which seems to address your immediate concern of setting the Ids of the domain objects is to use test fakes. This is discussed by RhysC here.

like image 104
martinl Avatar answered Sep 23 '22 05:09

martinl


IMO, your create test should:

  • add an entity to the repository
  • assert it was assigned an ID
  • retrieve the inserted entity using the ID
  • compare the initial entity with the read entity and make sure their properties are same

I've got many unit tests like yours the main difference is that I'm using a deep comparison method to compare object instances. For instance, my U tests look like this:

  • add an entity to the repository
  • retrieve the inserted entity using the ID
  • change some of the entity properties
  • update the entity in the repository
  • retrieve the updated entity using the ID
  • compare the updated entity with the read entity and make sure their properties are same (you can define a specific logic for properties that are not updatable)
like image 24
vc 74 Avatar answered Sep 23 '22 05:09

vc 74