我正在尝试写这样的东西:
myStub.Stub(_ => _.Create(Arg<Invoice>.It.Anything)).Callback(i => { i.Id = 100; return i; });
我想获取传递给模拟的实际对象,对其进行修改然后返回。
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
我无需添加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;
});