Search code examples
c#rhino-mocks

How to pass a parameter in an expectation to the return value in Rhino Mocks?


I want to mock this interface:

interface IA {
  IB DoSomething(IC arg)
}

in a way that simulates an implementation like this:

class A : IA {
    public IB DoSomething(IC arg) { return new B(arg); }
}

How can I do that? From other similar questions, it's supposed to be something like this:

MockRepository.GenerateMock<IA>().Expect(x => x.DoSomething(null)).IgnoreArguments().Callback<IC>(arg => new B(arg))

But i can't get it to work. I'm using RhinoMocks 3.6


Solution

  • Here is a typesafe example:

    var mockA = MockRepository.GenerateMock<IA>();
    mockA
        .Stub(x => x.DoSomething(Arg<IC>.Is.Anything))
        .Do((Func<IC, IB>)(arg => new B(arg)))
        .Return(null);