Say I have the following business logic:
foreach (var item in repository.GetAll())
{
if (SomeConditition)
{
item.Status = Status.Completed;
repository.Update(item);
}
}
Now I write the following unit test:
public void Test()
{
var repository = new FakeRepository();
repository.Add(new Item());
RunBusinessLogic(repository);
Assert.AreEqual(Status.Completed, repository[0].Status);
}
FakeRepository is implemented over a List, and GetAll() simply returns the list.
While I can test most of the logic involved using this approach, I cannot verify that I have remembered to call repository.Update() in the business code. Because FakeRepository.GetAll() returns the actual objects, the repository objects will already have been modified by business code before I call Update(). In fact, FakeRepository.Update() does nothing.
I realize I can use FakeRepository.Update to record that it has been called, an Assert that in the test. But if I forget to call Update, I cannot be trusted to remember to Assert the Update either, right? I would prefer the test to simply fail if the Update call was omitted.
Any ideas?