2011-12-01 52 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仍然會拋出一個無返回的異常,因此需要返回。 –