views:

63

answers:

2

I have the following test to verify that my repository is calling it's respective session (I've rewritten it to highlight the actual problem):

[Test]
    public void Why_Does_This_Fail()
    {
        var objectUnderTest = new SomeGenericsProblem();

        var fakeSession = MockRepository.GenerateMock<ISession>();
        fakeSession.Expect(s => s.Query<SomeClass>());

        objectUnderTest.NotWorking<SomeClass>();

        fakeSession.AssertWasCalled(t => t.Query<SomeClass>());
    }

but when I run the test I get this:

System.InvalidOperationException : Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method).(C#) / Overridable (VB) method).

Any ideas what I'm doing wrong here? The session that I'm mocking is an interface, so it has to be virtual/overridable.

I have a feeling it has something to do with the fact that my Query method is a generic, but I don't know any other way to express what I'm trying to test.

Also, If I remove the part that sets up the expectation (i.e. this line of code:)

fakeSession.Expect(s => s.Query<SomeClass>());

I get a different exception which is equally confusing to me:

System.InvalidOperationException : No expectations were setup to be verified, ensure that the method call in the action is a virtual (C#) / overridable (VB.Net) method calloverridable (VB.Net) method call

A: 
[Test]
public void Query_WhenCalled_CallsSessionQuery()
{
        // arrange
        var session = MockRepository.GenerateStub<ISession>();

        var r = new Repository(session);

        // act
        r.Query<SomeClass>();

        // assert
        session.AssertWasCalled(s => s.Query<SomeClass>());
}
Jay
Thanks for the thought Jay, but I rewrote the question to highlight what was going on, and removed the using clause entirely. The issue still remains.
Joseph
@Joseph The above works. Notice that I use `GenerateStub<>` instead of `GenerateMock<>`, and do not explicitly set up the expectation, as this is unnecessary.
Jay
@Jay Thanks again. I figured out what the issue was. The Query function is actually an extension method, and that's why I'm getting the error and you're not. You probably build your own ISession interface, whereas mine is coming from NHibernate.
Joseph
A: 

So I figured out what was wrong.

ISession comes from NHibernate, which I probably should have mentioned.

The reason why this is cruicialy important is because

session.Query<T> 

(which is what I'm trying to mock), is an EXTENSION METHOD.

Rhino Mocks apparently does not have the capability of mocking extension methods, hence why it's giving me the weird error.

So hopefully I'll have saves someone else the time and agony I've gone through in trying to figure out why my test won't pass.

The only solution that I've read about for this is to actually change the design of the extension method (which I can't do because it's part of NHibernate), or to use a different mocking framework like TypeMock.

Joseph