【问题标题】:RhinoMocks expectation with dynamic parameter动态参数的 RhinoMocks 期望
【发布时间】:2013-04-09 19:07:53
【问题描述】:

无论如何要模拟采用动态参数的方法吗?

我想设置这样的期望:

_hasher.Expect(h => h.ComputeHash(Arg<dynamic>.Matches(o=> o.PropertyA == "123"))).Return("some hash");

我收到错误:表达式树可能不包含动态表达式。我当然可以这样做:

_hasher.Expect(h => h.ComputeHash(Arg<object>.Is.Anything)).Return("some hash");

但我觉得这在我的测试中留下了一个空白。是否有其他替代方法可以模拟具有接受动态参数的方法的依赖项?

【问题讨论】:

    标签: unit-testing tdd rhino-mocks dynamicobject


    【解决方案1】:

    试试这个:

    _hasher.Expect(h => h.ComputeHash(Arg<object>.Is.Anything)).Return("some hash")
        .WhenCalled(x =>
            {
                dynamic actual = x.Arguments[0];
                Assert.AreEqual("123", actual.PropertyA);
            });
    

    当然,这有点像 hack,但它确实有效,并且在测试失败时会为您提供有用的消息。

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      相关资源
      最近更新 更多