RhinoMock:如何存根并返回以复杂对象作为参数的方法

发布于 2024-12-10 03:29:23 字数 804 浏览 6 评论 0原文

我非常感谢任何人可以帮助我解决以下问题: 我一直在单元测试中使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2024-12-17 03:29:23

我相信问题不在于复杂的数据类型,而在于您设定的期望。

第一次尝试修复此问题时,请在 Return 之前添加 IgnoreArguments()。您在期望中指定的 DataTable 可能与实际传入的 DataTable 实例不同,因此期望不会通过:

...Stub(...).IgnoreArguments().Return();

如果没有帮助,您可以手动调试它使用 WhenCalled():

...Stub(...).IgnoreArguments().WhenCalled(
    mi => 
    {
        var token = mi.Arguments[0] as TokenDataType;
        var dataTable = mi.Arguments[1] as DataTable;
    }).Return();

如果这没有帮助,请尝试在 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 the Return. It could be that DataTable you've specified in expectation differs from the actually-passed-in DataTable instance so expectations won't pass:

...Stub(...).IgnoreArguments().Return();

If not helped you can debug it manually using WhenCalled():

...Stub(...).IgnoreArguments().WhenCalled(
    mi => 
    {
        var token = mi.Arguments[0] as TokenDataType;
        var dataTable = mi.Arguments[1] as DataTable;
    }).Return();

If that doesn't help, try to add Repeat().Any() after the Return() 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.

豆芽 2024-12-17 03:29:23

如果它没有返回您期望的结果,则存根调用和实际调用之间的参数不匹配。假设你有这样的东西:

// Set expectations
var someDataTable = new DataTable(columns, raws);
mockRepository
   .Stub(repository => repository.Find(sessionToken, dataTable))
   .Return(new DeviceTypeRepository().NewTable(false));

// Actual test
var anotherDataTable = new DataTable(columns, raws);
yourTestObject.DoSomethingThatLooksForTheDataTable(repository);

这里的事情是,尽管 someDataTableanotherDataTable 具有完全相同的内容,但它们不是同一个对象,并且当 RhinoMocks 比较存根调用与实际调用的参数不匹配。你可以做的是使用约束:

mockRepository
   .Stub(repository => repository.Find(
      Arg<SessionID>.Matches(y => y.ID == 2),
      Arg<DataTable>.Matches(x => x.Columns == columns && x.Raws == raws)
   ))
   .Return(true);

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:

// Set expectations
var someDataTable = new DataTable(columns, raws);
mockRepository
   .Stub(repository => repository.Find(sessionToken, dataTable))
   .Return(new DeviceTypeRepository().NewTable(false));

// Actual test
var anotherDataTable = new DataTable(columns, raws);
yourTestObject.DoSomethingThatLooksForTheDataTable(repository);

The thing here that even though the someDataTable and anotherDataTable 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:

mockRepository
   .Stub(repository => repository.Find(
      Arg<SessionID>.Matches(y => y.ID == 2),
      Arg<DataTable>.Matches(x => x.Columns == columns && x.Raws == raws)
   ))
   .Return(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文