I'm testing the delete method of an abstract base repository class which is inherited by several other repository classes. MyTestRepository inherits the base so I can run tests against the base's methods without restricting my test to using a concrete class. When I run my unit test it passes, but I noticed afterwards I have several OrderDetail and Schedule objects in the test DB that were generated by the test (objects get created during test initialization) and not deleted, while the Order object is deleted. I added some breakpoints and noticed that as soon as the helper method ends and the expected exception has been thrown the test ends and the other calls to the helper never occur.
This is my first attempt at unit testing. Are my methodologies wrong? Is ExpectedException working as intended and I'm misusing it or is there another tool I should be using? The only way I can think of to get my test is to put a try catch block in the helper and assert true when I catch my DataAccessException.
[TestMethod]
[ExpectedException(typeof(DataAccessException))]
public void NHibernateRepositoryBaseDelete()
{
NHibernateRepositoryBaseDeleteHelper<Order, int>(myOrder, myOrder.OrderId);
NHibernateRepositoryBaseDeleteHelper<OrderDetail, int>(myOrderDetail, myOrderDetail.OrderDetailId);
NHibernateRepositoryBaseDeleteHelper<Schedule, int>(mySchedule, mySchedule.ScheduleId);
}
private static void NHibernateRepositoryBaseDeleteHelper<T, TKey>(T myItem, TKey myItemId)
{
MyTestRepository<T, TKey> myRepository = new MyTestRepository<T, TKey>();
myRepository.Delete(myItem);
myRepository.CommitChanges();
myRepository.GetById(myItemId, false);
}