I typically don't mock out the NHibernate objects directly, but rather the objects that consume the managers I have for creating the ISessionFactory and ISession.
For example, I have an interface called ISessionManager like so
Code:
public interface ISessionManager
{
ISession CreateSession();
}
The implementation of this would BuildSessionFactory (thus, it would be a Singleton) and also grab an ISession when I ask for it (scoping according to THreadLocal or HttpContext or CallCOntext).
Now, really when you are testing your business entities you shouldn't need to mock any of this since you would typically decouple the NHibernate specifics away from your Domain anyways.
So if I have an IRepository like so
Code:
public interface IRepository<T>
{
void Save();
void Delete();
void ScratchMyButt();
}
My implementation of IRepository may CONSUME my ISessionManager implementation (or just ISession), but my unit testing doesn't need to know all that if I am just testing the entities served by my REpository.
That's where mocking comes in.
I can use some Mock library (I use RhinoMocks or NMock2) to mock the Repository like so:
Code:
Mockery mocks = new Mockery();
IRepository<Thing> repos = mocks.CreateMock<IRepository<Thing>>();
Then I can place expectations on the methods to return static values or whatever like so:
Code:
Thing dummyObject = new Thing();
Expect.On(repos).Method("Get").Will(Return.Value(dummyObject));
Then I can inject this
repos into my Service object or whatever and run the test as expected without ever connecting to the DB.
This reallly has nothing to do with NHibernate at this point...just TDD.
Hope this helps...
MIKE
[/b]