Search code examples
rhino-mocks

Rhino.Mocks how to test abstract class method calls


I'm trying to test if the method I want to test calls some external (mock) object properly.

Here is the sample code:

using System;

using Rhino.Mocks;

using NUnit.Framework;

namespace RhinoTests
{
    public abstract class BaseWorker
    {
        public abstract int DoWork(string data);
    }

    public class MyClass
    {
        private BaseWorker worker;
        public BaseWorker Worker
        {
            get { return this.worker; }
        }

        public MyClass(BaseWorker worker)
        {
            this.worker = worker;
        }

        public int MethodToTest(string data)
        {
            return this.Worker.DoWork(data);
        }
    }

    [TestFixture]
    public class RhinoTest
    {
        [Test]
        public void TestMyMethod()
        {
            BaseWorker mock = MockRepository.GenerateMock<BaseWorker>();
            MyClass myClass = new MyClass(mock);

            string testData = "SomeData";
            int expResponse = 10;

            //I want to verify, that the method forwards the input to the worker
            //and returns the result of the call

            Expect.Call(mock.DoWork(testData)).Return(expResponse);
            mock.GetMockRepository().ReplayAll();

            int realResp = myClass.MethodToTest(testData);

            Assert.AreEqual(expResponse, realResp);
        }
    }
}

When I run this test, I get:

TestCase 'RhinoTests.RhinoTest.TestMyMethod'
failed: 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).
    at Rhino.Mocks.LastCall.GetOptions[T]()
    at Rhino.Mocks.Expect.Call[T](T ignored)
    RhinoTest.cs(48,0): at RhinoTests.RhinoTest.TestMyMethod()

The exception is thrown on the Expect.Call line, before any invocation is made.

How do I approach this - i.e. how to check if the method under test properly forwards the call?

This is .Net 2.0 project (I can no change this for now), so no "x =>" syntax :(


Solution

  • I have to admit, I'm not entirely sure what's going on here, but using Rhino.Mocks 3.6 and the newer syntax, it works fine for me:

    [Test]
    public void TestMyMethod()
    {
        MockRepository mocks = new MockRepository();
        BaseWorker mock = mocks.StrictMock<BaseWorker>();
    
        MyClass myClass = new MyClass(mock);
        string testData = "SomeData";
        int expResponse = 10;
    
        using (mocks.Record())
        {
            //I want to verify, that the method forwards the input to the worker
            //and returns the result of the call
            Expect.Call(mock.DoWork(testData)).Return(expResponse);
        }
    
        using (mocks.Playback())
        {
            int realResp = myClass.MethodToTest(testData);
            Assert.AreEqual(expResponse, realResp);
        }
    }
    

    It doesn't have anything to do with the Rhino.Mocks version. With the old syntax, I get the same error as you're getting. I didn't spot any obvious errors in your code, but then again, I'm used to this using syntax.

    Edit: removed the var keyword, since you're using .NET 2.0.