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);
Question
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);
OTHER TIPS
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);