0

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

4

2 回答 2

1

这是一个类型安全的示例:

var mockA = MockRepository.GenerateMock<IA>();
mockA
    .Stub(x => x.DoSomething(Arg<IC>.Is.Anything))
    .Do((Func<IC, IB>)(arg => new B(arg)))
    .Return(null);
于 2013-08-12T18:43:02.713 回答
1
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);
于 2013-08-08T14:19:28.810 回答