Reputation: 494
I've just been handed an unfinished asp.Net MVC 3 / nHibernate project to fix up and complete, and I'm in the process of trying to get the nUnit unit tests that are already there to pass.
The repository tests are the ones causing issue. All I'm trying to do is add a couple of items to a test repository but I'm darned if I can figure out how to do that. Situation is below:
[Test]
public void CanGetAllThings()
{
ServiceLocatorInitializer.Init();
ThingRepository thingRepository = new ThingRepository(); // ThingRepository inherits from NHibernateRepository<T>, IRepository<T>
Thing thingA = new Thing { /*initialization values*/ };
Thing thingB = new Thing { /*initialization values*/ };
componentAuthorizationRepository.DbContext.BeginTransaction();
ThingRepository.Save(ThingA); // Doesn't appear to be doing anything
ThingRepository.Save(ThingB);
componentAuthorizationRepository.DbContext.CommitTransaction();
ThingRepository.GetAll().ShouldNotBeNull(); // Passes
ThingRepository.GetAll().Count.ShouldEqual(2); // Fails with actual value 0
}
Am I supposed to use a mocking framework to create an in memory database? It seems like that's overkill for such a simple test and that there should just be a Repository.Add() or Repository.Save() method but I'm obviously missing something crucial here.
I'm a junior developer and new to MVC, nHibernate and in fact unit testing so I'm not even clear on where my knowledge gap is so I can research! Any pointers hugely appreciated.
Upvotes: 1
Views: 282
Reputation: 5427
Since you have commented out the transaction, your Save() operations are not being committed. Somehow you need to add transaction management to your repository, or have a Unit of Work to manage it for you.
Or, if you want quick and dirty, you need a way to call Flush() on the repository's ISession.
Upvotes: 1