I am a newbie so this is essentially a best practice question. I have a simple object, unit tests for the object in a VS Unit Test project and an NHibernateSessionManager object that does basic BuildSessionFactory(), OpenSession(), BeginTransaction(), etc. calls.
What I thought would be correct is for the unit test fixture to start a single transaction in the ClassInitialize method, perform the various database requests/changes in all the tests then Rollback the single transaction in the ClassCleanup method but it does not work. It appears to work if I roll the transaction back because nothing is left in the SQL database but the changes are not commited even if I test with CommitTransaction instead. Walking through the logic the transaction is no longer active after one test has been completed.
I can make it work correctly by using a separate transaction for each test...that is, wrap each test in Begin/CommitTransaction but that obviously creates a separate transaction for each test.
Is a separate transaction for each test the correct solution or am I doing something wrong to make the transaction close before I expect? The answer does not seem as important for the unit tests but makes me wonder why the transaction is not active and how that would affect the "real" application.
|