简体   繁体   中英

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

var mock = MockRepository.GenerateMock<IA>();
mock
  .Stub(x => x.DoSomething(Arg<IC>.Is.Anything)
  // return a new instance of B each time
  .WhenCalled(call => call.ReturnValue = new B((IC)call.Arguments[0]))
  // make rhino mock validation happy
  .Return(null);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM