c# – Rhino Mocks接收参数,修改它并返回?

我试图写这样的东西:

myStub.Stub(_ => _.Create(Arg<Invoice>.It.Anything)).Callback(i => { i.Id = 100; return i; });

我想获得传递给mock的实际对象,修改它并返回。

Rhino Mocks可能是这种情况吗?

你可以使用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
http://stackoverflow.com/questions/8343709/rhino-mocks-recieve-argument-modify-it-and-return

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:c# – Rhino Mocks接收参数,修改它并返回?