In a (unit) test I sometimes add extra tests to check the test tests what is anticipated.
user = new User(Id="A", Name = "N1")
sut.Create(user)
saved = sut.Get("A")
saved.Name.Should.Be("N1", "Sanity check we know what we have stored.")
// Act.
sut.Clear("A")
// Assert.
result = sut.Get("A")
result.Name.Should.Be(null)
Notice the “sanity check” to verify a bug didn’t set Name to null when creating the user.
My naming convention is “Sanity check” or “Nykterhetskontroll” in Swedish. [I know the translation, word per word, is incorrect.]
In an O/RM solution the business logic often ends up in the very queries. Don’t mock them if you want to test business logic.
TL;DR
Some hundred years ago when Microsoft was pushing their first Entity framework and all examples were PresentationLayer<->DataAccessLayer someone asked “When one is using an O/RM; where is the business logic?”
After all this time I have finally found out. In the very queries.
This effects the unit tests.
Because what you often want to test with unit tests is business logic and mock out everything else. The call to the O/RM is often quite simple. Then comes some hefty Linq statement that translates to EF or nHibernate. Then follows some simple handling of the result. The main logic is in the O/RM call.
So when one mocks out the O/RM one mocks out a big part of the logic.
One solution is to not mock out the O/RM but instead mock the data retrieval beneath. I have not figured out how to do this.
Another solution is to do an integration test all the way to the database. My first try at this was messy but worked out. My second try was equally messy but also worked out and has returned the investment many times. My third, and present, try uses helper methods to set up the data and contrary to the earlier tries it doesn’t look messy any more.
This is inspired by my present gig where the QA guy wrote a simple DSL with fluent syntax to set up test data. I have not managed to replicate his library but am, presently, happy with helper methods.