henginy
henginy

Reputation: 2071

How to use the rollback pattern with NHibernate transactions?

I'd like to use the rollback pattern for testing my services. These are actually logical services that manage transactions and handle all repository access, called by a Winform UI. The repositories use NHibernate for database operations.

Given NHibernate doesn't support nested transactions, it doesn't work to begin a transaction in test setup and rollback in tear down. My transactions executed in-between were not rolled back.

The only way I've found is changing the service class so that I can inject to it the ITransaction created in test setup. But this method exposes the transaction to the UI as a side-effect and feels like changing my implementation just for testing.

Is there a better way to achieve this? Any suggestions or directions are welcome.

Upvotes: 0

Views: 746

Answers (1)

tmesser
tmesser

Reputation: 7666

I ran into this same problem at one of my previous jobs, and honestly the situation was so thorny we ended up ducking the problem by having a test database.

The basic idea is that you have some base-line snapshots or base setup script for your database, which you run on your test server daily, hourly, or whenever the tests run - whatever's appropriate. The way we handled it was to have the SetUp method to reset to baselines by invoking a script; we would then do all our tests, and reset to baselines a second time during the TearDown procedure (if we needed).

You can even set up the baseline location as a parameter so you can pull in baselines from QA for changes that shouldn't break anything, and local baselines if you're testing changes. All in all, it was a little awkward to get used to, but it worked well and did not bog down our development process.

Upvotes: 1

Related Questions