3

I am trying to mock out or similar the Query extension method applied to an nhibernate-3 session. Similar to the following...

public IQueryable<Entity> GetEntities
{
  return entities = Session.Query<Entity>();
}

Where the Query extension method returns an IQueryable collection.

I can clearly mock out the Session object by using Moq or Rhinos. But as of yet not found a suitable way to fake out or replace the extension method. In fact reading answers to several questions on stack overflow, it may not be possible.

see questions How to mock extension methods with Rhino Mock? Mocking Extension Methods with Moq and links within..

I came across the microsoft moles project and thought maybe I could use that to create a subsitute assembly by which I could test this one particular method. Using Pex its relatively quite easy to generate such an assembly however when I try to work a way of using this Assembly its not immediately clear of a way to factor this out.

Ideally I would like to just check to make sure that the query extension method is called by this method and a pre-empted result is returned..

Has anyone done anything similar, would like to hear peoples thoughts..

Ps. there is type mock but I don't have the funds to buy it :)

Community
  • 1
  • 1
Aim Kai
  • 2,934
  • 1
  • 22
  • 34

1 Answers1

2

Extension methods are static methods. So you can't use a mocking library like Rhino Mocks or Moq. (This is a CLR limitation.) TypeMock or Moles get around the limitation by wedging themselves in at the profiler level. (They register themselves as a profiler and inject code into your app domain.)

You can just define your own extension method with the same signature as found in NHibernate.Linq.LinqExtensionMethods:

// NOT RECOMMENDED!!!
public static class MockedSessionExtensions {
    public static IQueryable<T> Query<T>(this ISession session) {
        // your mocked impl goes here
    }
}

The problem with this is it has to be in your production code with some conditional compilation directives around it.

Personally I integration test my repositories against a local database or an in-memory database like SQLite. I then mock out my repositories when testing higher level components. I wouldn't recommend mocking out ISession, ISessionFactory, and the like.

James Kovacs
  • 11,549
  • 40
  • 44
  • James, I agree with you, and I do use integration tests to test the repository layers. I use sqlite to do the checking of the mappings and sql server for repositories. I was interested if someone had found a way to do this via unit testing thats all. – Aim Kai Nov 18 '10 at 10:10
  • My question to you is what would you gain from unit testing your repositories? If you feel it's worth the effort, create an IGenericRepository (or something similar) that encapsulates the session and mock that. – James Kovacs Nov 18 '10 at 16:17
  • That is pretty much what I have done. But of course I got to a point with the Query extension method and hence the question. Answering what would I gain from unit testing the repository, I think it is important especially with a fundamental part of your application to test this where possible without relying on any external components or dependencies. I think its useful to test any data specific code that enhances nhibernate or uses it. – Aim Kai Nov 19 '10 at 09:41
  • Whilst this is an interesting answer, it does not answer the actual question. – ilivewithian Nov 19 '10 at 13:46