简体   繁体   English

Rhino Mocks:如何模拟出可以转换其输入的方法?

[英]Rhino Mocks: How can I mock out a method that transforms its input?

I have a Data Access Object TransactionDao. 我有一个数据访问对象TransactionDao。 When you call TransactionDao.Save(transaction) I would like for it to setting a transaction.IsSaved=true flag (this is a simplification the actual thing I'm trying to do is not quite so banal). 当您调用TransactionDao.Save(transaction)时,我希望它设置一个transaction.IsSaved = true标志(这是一种简化,实际上我想做的事情并不是那么平庸)。 So when mocking my TransactionDao with RhinoMocks how can I indicate that it should transform its input? 因此,当使用RhinoMocks模拟我的TransactionDao时,如何指示它应该转换其输入?

Ideally I would like to write something like this: 理想情况下,我想写这样的东西:

Expect.Call(delegate {dao.Save(transaction);}).Override(x => x.IsSaved=true);

Does anyone know how to do this? 有谁知道如何做到这一点?


Though I got a hint how to do it from the answer specified below the actual type signature is off, you have to do something like this: Because of what Mark Ingram posted, seems like the best answer, though nobody's explicitly said it, is to do this: 尽管从实际类型签名下面指定的答案中我得到了提示,但您必须执行以下操作:由于Mark Ingram发表的内容似乎是最佳答案,尽管没有人明确说过,但这是做这个:

public delegate void FakeSave(Transaction t);
...
Expect.Call(delegate {dao.Save(t); }).Do( new FakeSave(delegate(Transaction t2) { t.IsSaved = true; }));

Gorge, 峡谷,

The simplest solution, which I found, applied to your question is the following: 我发现,最简单的解决方案适用于您的问题:

Expect.Call(() => dao.Save(transaction))
    .Do(new Action<Transaction>(x => x.IsSaved = true));

So you don't need to create a special delegate or anything else. 因此,您无需创建特殊的委托人或其他任何东西。 Just use Action which is in standard .NET 3.5 libraries. 只需使用标准.NET 3.5库中的Action。

Hope this help. 希望对您有所帮助。 Frantisek 弗朗齐歇克

You can accomplish this using the Do callback: 您可以使用Do回调完成此操作:

Expect.Call(delegate {dao.Save(transaction);})
    .Do(x => x.IsSaved = true);

you should mock the transaction and make it return true fo IsSaved, if you can mock the transaction of course. 如果可以模拟交易,则应该模拟交易并使IsSaved返回true。

ITransaction transaction = _Mocker.dynamicMock<ITransaction>;
Expect.Call(transaction.IsSaved).IgnoreArguments.Return(true);
_mocker.ReplayAll();
dao.Save(transaction);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM