如何查找已传递给我的模拟(Moq 或 Rhino Mocks)接口上的方法的值?
我正在使用Moq - 但可以轻松地切换到另一个模拟框架如果需要。
我定义了一个接口:
public interface IBaseEngineManagerImp
{
void SetClientCallbackSender(IClientCallbackSender clientCallbackSender);
}
然后我用 engineManager 模拟 IBaseEngineManagerImp
mockEngineManagerImp = new Mock<IEngineManagerImp>();
EngineManager engineManager = new EngineManager(mockEngineManagerImp.Object);
,然后调用 SetClientCallbackSender 传入一个值。
如何获取从单元测试传递给 SetClientCallbackSender 的值?
(我希望在 clientCallbackSender 上调用一些方法作为测试的一部分)
I am using Moq - but could easily swap to another mock framework if needed.
I have a interface defined:
public interface IBaseEngineManagerImp
{
void SetClientCallbackSender(IClientCallbackSender clientCallbackSender);
}
I then mock IBaseEngineManagerImp with
mockEngineManagerImp = new Mock<IEngineManagerImp>();
EngineManager engineManager = new EngineManager(mockEngineManagerImp.Object);
engineManager then calls SetClientCallbackSender passing in a value.
How do I get the value that was passed to SetClientCallbackSender from my unit test?
(I wish to call some methods on clientCallbackSender as part of the test)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在模拟上使用 .Callback 方法,以调用传入 SetClientCallbackSender 方法的参数上的任何函数:
you can use the .Callback method on the mock, to call any function on the parameter that was passed in to the SetClientCallbackSender method:
在rhino中,您使用
WhenCalled
或GetArgumentsForCallsmadeOn
:此实现的问题是,您只获得最新的参数。您可以通过使用参数约束(而不是 Is.Anything)对此进行更多控制。
或
GetArgumentsFormCalsMadeOn 的问题是,它返回一个二维数组,每个调用对应一行,每个参数对应一列。因此,您必须确切地知道被测设备执行了多少次调用。
In rhino, you use
WhenCalled
orGetArgumentsForCallsmadeOn
:The problem with this implementation is, that you just get the latest argument. You could put more control to this by using argument contraints (instead of Is.Anything).
or
The problem with the
GetArgumentsFormCalsMadeOn
is, that it returns a two dimensional array, a row for each call and a column for each argument. So you have to know exactly how many calls your unit under test performs.