Having just had the unfortunate need to use rhino mocks from managed C++ I thought I would document the “how”

Let’s say you want to mock/stub IFoo. Which has two methods:

IQux Bar()
int Baz(int)

Create a MockRepository:

IFoo^ foo_stub = MockRepository::GenerateStub<IFoo^>();

Stub out Bar() -> Note it doesn’t take a parameter:

Rhino::Mocks::Function<IFoo^, IQux^>^ foo_bar =
    gcnew Rhino::Mocks::Function<IFoo^, IQux^>(&IFoo::Bar);

RhinoMocksExtensions::Stub(foo_stub, foo_bar)->Return(
    /* Another mock/stub or a real IQux */
);

Stub out Baz() -> Note it takes a parameter:

Note, the static method, this will configure the stub for a specific argument, in this example when Baz(1) is called we return 2.

Rhino::Mocks::Function<IFoo^, int>^ foo_baz =
    gcnew Rhino::Mocks::Function<IFoo^, int>(&Test::FooStubBaz);

RhinoMocksExtensions::Stub(foo_stub, foo_baz)->Return(2);

// IFoo will actually be a rhino mock/stub
int Test::FooStubBaz(IFoo^ foo) {
    return foo->Baz(1);
}

Now foo_stub->Baz(1) will return 2

I imagine you could remove the need for a static method but this was enough for my needs and my head!