views:

1469

answers:

4

It seams to me as most people write their tests against in-memory, in-process databases (like SQLite) when working with NHibernate, which is fine. I have this up and running but my first test (that uses NHibernate) always takes between 3-4 seconds to execute, next test runs much faster.

I am using FluentNhibernate to do the mapping but get ruffly the same timings with XML mapping files. For me personally 3-4 second test execution seriously disrupts my flow.

What is the recomended way of working with TDD and NHibernate?

Is it possible to mock ISession to unit test the actuall queries or can this only be done with in memory databases?

+1  A: 

See http://www.autumnofagile.net and http://www.summerofnhibernate.com

Webjedi
interesting links... thanks
bob
Cool links, and they do have relation to nHibernate, but what specifically do provide to answer the question?
David
In the series he uses TDD and shows it in action.
Webjedi
A: 

Have you tried changing some of the defaults in the optional configuration properties? The slowdown is most likely related to certain optimizations nhibernate does with code generation.

http://nhforge.org/doc/nh/en/index.html#configuration-optional

It seems like an in memory db is going to be the fastest way to test a your data layer. It also seems once you start testing your data layer you're moving a little beyond the realm of a unit test.

Min
+6  A: 

I am using the Repository Pattern to perform Database operations, and whenever I run my Tests I just run the higher-level tests that simply Mock the Repository (whit RhinoMocks).

I have a seperate suite of tests that explicitly tests the Repository layer and the NHibernate mappings. And those usually don't change as much as the business and gui logic above them.

That way I get very fast UnitTests that never hit the DB, and still a well tested DB Layer

Tigraine
Those separate tests are more in the realm of integration tests anyway. Nice that you can automate them but it's good to keep them separate. ^1
Mendelt
Yeah, nhibernate makes it very easy to do those tests by having the database re-created every time you start a test. That takes time and my NHibernate Tests take about a Minute to run, but still, they run and I know when things don't work.
Tigraine
how about testing method that require query data or save operation that require foreign key etc i could imagine it a lot of work for loading data for testing
c.sokun
+1  A: 

Unit testing data access is not possible, but you can integration test it. I create integration test for my data access in a seperate project from my unit tests. I only run the (slow) integration tests when I change something in the repositories, mapping or database schema. Because the integration tests are not mixed with the unit tests, I can still run the unit tests about 100 times a day without getting annoyed.

Paco
I'm not sure you are correct about that. It really depends on HOW you write tests. If your tests are Atomic, Order indepenent and isolated, Intention Revealing, Easy to setup and Fast. They are in every sense UnitTests. See: http://codebetter.com/blogs/jeremy.miller/archive/2005/07/20/129552.aspx
maz
Some test that interacts with the file system, a database or any other system is not independend, so not a unit test. I know J.Miller agrees with that. With fast is meant able to run thousend of tests in 10 seconds. Whatever you do, you won't reach that speed with db integration tests.
Paco
Yes, I agree that "...test that interacts with the file system, a database or any other system is not independend..." but what I am talking about here is to run against an in-process, in-memory db. This is no different from any other in-memory object you might have in your tests today.
maz