Given the following code:
public void UpdatePerson(int id, string name)
{
Person p = peopleRepository.Get(id);
p.name = name;
peopleRepository.Update(p);
}
One answer would be (using a pseudo mocking framework):
Person p = new Person(); Expect.Call(peopleRepository.Get(0))
.Returns(p);
Expect.Call(peopleRepository.Update(p));
...
service.UpdatePerson(0, "MyName");
Other approach would be (using pseudo coding again):
Person p = CreateAndInsertToDB();
service.UpdatePerson(p.id, "New Name");
FlushAndRecreateTheSession();
Person updated = GetFromDB(p.Id);
Assert.Equal("New Name", updated.Name);
What would you do, and why?
(I'm tagging that also under altnetuk as it has been inspired by a session around test-granularity, mocking frameworks, etc.)
Usually I won't just link to another's post, but this one is a very good introductory level Unit Testing 101 Article, covering basics of the idea, and basic of nUnit and Rhino.Mocks.
A must read for Unit Testing newbies.