我正在尝试写这样的东西:
myStub.Stub(_ => _.Create(Arg<Invoice>.It.Anything)).Callback(i => { i.Id = 100; return i; });
我想获得传递给mock的实际对象,修改它并返回。
这种情况是否适用于Rhino Mocks?
答案 0 :(得分:93)
你可以像这样使用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 :(得分:0)
我没有添加IgnoreArguments()来避免使用Return()。 这是我原来的方法:
List<myEntity> GetDataByRange(int pageSize, int offsetRecords);
这是我的模拟示例:
_Repository.Stub(x => x.GetDataByRange(Arg<int>.Is.Anything, Arg<int>.Is.Anything))
.WhenCalled(x => {
var mylist = entitiesList?.Skip((int)x.Arguments[1])?
.Take((int)x.Arguments[0])?.ToList();
x.ReturnValue = mylist;
});