I am a new comer to NHibernate (+Fluent), and that i can't choose what is the best strategy if this involves constructing my code to really make it testable. I've got a plain structure together with a domain model, mappings to map the model to database, along with a couple of classes with behavior that will work at the model classes and do transactions for upgrading and reading through out data in the database. I've more on the top of the, but that is not relevant here.

Now, if this involves the system testing what appears natural in my experience would be to inject something towards the behavior classes such that they'll obtain a SQLite database for testing, or even the real database this really is set up. Performs this seem sensible? Things I can't decide is what to inject.

  • I possibly could inject the ISession, however i assume I'd desire to use new periods for every - well session? If that's the case I'd need something outdoors to produce the periods, that might just slowly move the problem?
  • Could I inject the SessionSource? Within this situation the behaviour classes could create periods themselves by using this. However, I begin to see the SessionSource can be used to BuildSchema - something I suppose I simply want to do once?
  • The 3rd option - and what I have carried this out far - is to produce a SessionFactory which produces a session for me personally. By using this I'm able to give a using-clause and also have the session disposed once the function finishes. I've got a SessionFactory-interface along with a regular along with a unittest implementation. Inserting either the standard or unittest implementation I recieve a session for the best database.

Shall We Be Held on course? How must i strcuture my code for writing unit tests for code using NHibernate? Any guidelines I should know?

You might want to read this blog publish by Ayende: Nhibernate Unit Testing. He's an evaluation base class that configures the session by having an in memory database (sql lite). This appears be much like your third option.