c# - Rhino Mocks - 使用 Arg.Matches

标签 c# .net unit-testing rhino-mocks

我有一个正在模拟的函数,它将参数对象作为参数。我想根据对象中的值返回结果。我无法比较这些对象,因为没有覆盖 Equals。

我有以下代码:

_tourDal.Stub(x => x.GetById(Arg<TourGet>.Matches(y => y.TourId == 2), null)).Return(
                new Tour() 
                {
                    TourId = 2,
                    DepartureLocation = new IataInfo() { IataId = 2 },
                    ArrivalLocation = new IataInfo() { IataId = 3 }
                });

当提供的参数的 TourId 为 2 时,这应该返回指定的对象。

这看起来应该可以,但是当我运行它时,出现以下异常:

When using Arg, all arguments must be defined using Arg.Is, Arg.Text, Arg.List, Arg.Ref or Arg.Out. 2 arguments expected, 1 have been defined.

有什么想法可以解决这个问题吗?

最佳答案

您需要对第二个 null 参数使用相同的语法,大致如下(我还没有测试过):

_tourDal.Stub(x => x.GetById(Arg<TourGet>.Matches(y => y.TourId == 2), Arg<TypeName>.Is.Null)).Return(
            new Tour() 
            {
                TourId = 2,
                DepartureLocation = new IataInfo() { IataId = 2 },
                ArrivalLocation = new IataInfo() { IataId = 3 }
            });

关于c# - Rhino Mocks - 使用 Arg.Matches,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3520911/

相关文章:

c# - 这段代码需要太长时间的原因是什么?

c# - 为什么面板在移除它及其子面板时会闪烁?

C# Buddy 类/元数据和反射

javascript - 如何测试YUI3的get()方法返回的值?

unit-testing - DSL生成测试数据

.net - 如何从 WatiN 链接对象获取原始 href 属性?

c# - 数据传输对象模式

c# - "Cartiesian Product"C++ 方法

c# - EditorTemplate继承——有没有办法

.net - Listview 列标题不显示 VB.Net