0

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

Joseph
  • 25,330
  • 8
  • 76
  • 125
  • I'm not seeing anything wrong in your test. Have you tried to remove the UnitOfWork and see if it helps? I'm wondering if there's something inside your Repository.Query or uow.Commit-methods which make the test fail. I copy-pasted your test method into my own project and let the ReSharper to create the UnitOfWork, Repository and ISession. The test passed OK. I'm using version 3.5 of Rhino Mocs. – Mikael Koskinen Sep 09 '10 at 16:55
  • @Mikael I have to have the unit of work in there, because if it's not then when I create the repository it will throw an invalid operation exception (by design). I removed the commit, and I get the same thing. My repository.Query method literally just calls Session.Query and that's all it does. – Joseph Sep 09 '10 at 17:06
  • I just rewrote the test from scratch using the same design but with my own POCOs and it's still giving me the same error. – Joseph Sep 09 '10 at 18:28

2 Answers2

2

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
  • 25,330
  • 8
  • 76
  • 125
0
[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
  • 56,361
  • 10
  • 99
  • 123
  • 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 Sep 09 '10 at 19:02
  • @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 Sep 09 '10 at 19:21
  • 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 Sep 09 '10 at 19:27