【问题标题】:Unit test: Check which expression was passed into method in test单元测试:检查哪个表达式被传递到测试中的方法中
【发布时间】:2015-05-14 19:48:53
【问题描述】:

我目前正在研究如何最好地测试以下代码,其中调用相同的方法,只是使用不同的表达式。

if (boolResult)
{
    service.Update(
        x => x.Id == newId && x.Version == version,
        x => new Foo
        {
            FooId = newId,
            OtherProp = otherValue
        });
}
else if (some other scenario)
{
    service.Update(x => x.Id == newId,
        x => new Foo
        {
            FooId = newId
        });
}

在单元测试中,我想断言service.Update 调用是使用预期的表达式运行的。我想要实现的是如下所示:

//set up the expressions
Expression<Func<Foo, bool>> expression = x => x.Id == newId && x.Version <= newVersion;
Expression<Func<Foo, Foo>> updateExpression = x => new Foo
{
    Id = newId,
    OtherProp = otherValue
};

//assert
m_service.AssertWasCalled(x => x.Update(Arg<Expression<Func<Foo, bool>>>.Is.Equal(expression), Arg<Expression<Func<Foo, Foo>>>.Is.Equal(updateExpression)));

这失败了,但想知道这种方法是否正确,以及如何最好地检查正确的表达式是否已传递到函数中。 请注意,在测试中,m_service 是 MockRepository.GenerateMock&lt;IService&gt;();

【问题讨论】:

    标签: c# nunit rhino-mocks


    【解决方案1】:

    不要验证被测单元的内部;而是验证其效果。

    一个表达式做另一个不做的事情:它更新另一个属性。验证此属性是否已更新。

    【讨论】:

      猜你喜欢
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多