2011-12-01 59 views

回答

82

你可以使用WhenCalled方法是这样的:

myStub 
    .Stub(_ => _.Create(Arg<Invoice>.Is.Anything)) 
    .Return(null) // will be ignored but still the API requires it 
    .WhenCalled(_ => 
    { 
     var invoice = (Invoice)_.Arguments[0]; 
     invoice.Id = 100; 
     _.ReturnValue = invoice; 
    }); 

,然后你可以创建你的存根,例如:

Invoice invoice = new Invoice { Id = 5 }; 
Invoice result = myStub.Create(invoice); 
// at this stage result = invoice and invoice.Id = 100 
+1

您可以通过添加IgnoreArguments避免调用返回()()最后我想。 – samjudson

+2

@samjudson:即使使用IgnoreArguments,Rhino仍然会抛出一个无返回的异常,因此需要返回。 –