RhinoMock:如何存根并返回以复杂对象作为参数的方法
我非常感谢任何人可以帮助我解决以下问题: 我一直在单元测试中使用RhinoMock。 我以这种方式定义我的模拟对象,sessionToken 是字符串类型的:
mockRepository.Stub(repository => repository.FindById(sessionToken)).Return(new DeviceTypeRepository().NewTable(false));
调用 FindById() 时代码部分可以返回 valid new new DeviceTypeRepository() .NewTable(假);
但是,当将复杂参数作为对象(例如 DataTable)包含到存根中时,如下所示:
mockRepository.Stub(repository => repository.Find(sessionToken, dataTable)).Return(new DeviceTypeRepository().NewTable(false));
然后调用 Find() 的代码部分,它不返回预期的 new DeviceTypeRepository().NewTable(false)。 请注意,参数 dataTable 的输入值在 Stub 和 Find() 调用中是相同的。
因此,我的问题是: 如何使用 RhinoMock 将此类参数(DataTable 类型和更一般的参数)实现到存根初始化中?我将不胜感激任何建议和方法。 谢谢
I highly appreciate anyone can help me in below-mentioned issue:
I've been using RhinoMock in Unit Test.
I define my mock object in such manner, with sessionToken is string-typed:
mockRepository.Stub(repository => repository.FindById(sessionToken)).Return(new DeviceTypeRepository().NewTable(false));
It's ok for the code section when calling FindById() to return the valid new new DeviceTypeRepository().NewTable(false);
However, when include a complex parameter as object, such as a DataTable, into the Stub as below:
mockRepository.Stub(repository => repository.Find(sessionToken, dataTable)).Return(new DeviceTypeRepository().NewTable(false));
Then the code section in which Find() is invoked, it does NOT return expected new DeviceTypeRepository().NewTable(false).
Notice that input value of parameter dataTable is the same in both Stub and in Find() invocation.
Hence, my question is:
How could I implement such parameter (DataTable typed and more generally) into Stub initialization using RhinoMock ? I'd be grateful to any advice and approach.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信问题不在于复杂的数据类型,而在于您设定的期望。
第一次尝试修复此问题时,请在
Return
之前添加IgnoreArguments()
。您在期望中指定的DataTable
可能与实际传入的DataTable
实例不同,因此期望不会通过:如果没有帮助,您可以手动调试它使用 WhenCalled():
如果这没有帮助,请尝试在
Return()
之后添加Repeat().Any()
并查看它是否有效。我假设如果该方法被调用几次,您可能会错过第一个返回值,但我可能是错的。I believe the problem is not in a complex datatype but rather in the expectations you've set.
As a first attempt to fix it, add
IgnoreArguments()
before theReturn
. It could be thatDataTable
you've specified in expectation differs from the actually-passed-inDataTable
instance so expectations won't pass:If not helped you can debug it manually using WhenCalled():
If that doesn't help, try to add
Repeat().Any()
after theReturn()
and see whether it works. I am supposing that if the method was called a few times, you may have missed the first return value, but I may be wrong.如果它没有返回您期望的结果,则存根调用和实际调用之间的参数不匹配。假设你有这样的东西:
这里的事情是,尽管
someDataTable
和anotherDataTable
具有完全相同的内容,但它们不是同一个对象,并且当 RhinoMocks 比较存根调用与实际调用的参数不匹配。你可以做的是使用约束:If it doesn't return what you'd expect, then the parameters between the stub call and the actual call don't match. Let's say you have something like this:
The thing here that even though the
someDataTable
andanotherDataTable
have the exact same content, they're not the same object and when RhinoMocks compare the stub call to the actual call the parameters don't match. What you can do is use constraints: