Rhino Mocks Record Playback syntax

rhino-mocks

Help, can anyone help out and explain the purpose of the Rhino Mocks 'Record' scope?

I assumed that that the expectation set within the scope would only be verified but it seems as soon as you create the mock object, Rhino Mocks is in 'record mode' so I'm now unsure of the purpose of the Record scope.

Here's an example I have:

    private static void SomeTest()
    {
        MockRepository mockRepository = new MockRepository();
        ISomeInterface test = mockRepository.StrictMock<ISomeInterface>();

        test.Bar();

        using (mockRepository.Record())
        {
            Expect.Call<string>(test.GetFoo()).Return("Hello");
        }

        using (mockRepository.Playback()) 
        {
           test.GetFoo();
        }
    }

    public interface ISomeInterface
    {
        string GetFoo();
        void Bar();
    }

This test would fail because there is an expectation that Bar should be called. Is it because I've created a StrictMock and not Dynamic?

Best Answer

This test will fail because there is no expectation that Bar() would be called, but it was called.

To answer your question, yes, it is because you have a strict mock. If you change to a DynamicMock, it will ignore everything except the expectations that you actually set. I would highly recommend using DynamicMocks wherever possible, as StrictMocks are actually quite brittle and tend to end up being a lot of hassle.

As for Record/Replay, it is not automatically in Record mode if you're using a concrete MockRepository. It's just the nature of the StrictMock that looks for anything being called that was outside of expectations, no matter when.

Related Topic