正确设置了验证传递给Mock的参数的正确方法

问题描述

|| 如果以后验证方法已被调用,则在回调中进行断言是否可以接受?这是确保我的模拟程序获得期望的参数的首选方法,还是应该在回调中设置局部变量并对该实例执行断言? 我遇到一种情况,在Presenter类中有一些逻辑,该逻辑根据输入来派生值并将它们传递给Creator类。为了测试Presenter类中的逻辑,我想验证在调用Creator时是否遵守了正确的派生值。我想出了下面的示例,但是我不确定我是否喜欢这种方法:
[TestFixture]
public class WidgetCreatorPresenterTester
{
    [Test]
    public void Properly_Generates_DerivedName()
    {
        var widgetCreator = new Mock<IWidgetCreator>();
        widgetCreator.Setup(a => a.Create(It.IsAny<Widget>()))
                     .Callback((Widget widget) => 
                     Assert.AreEqual(\"Derived.Name\",widget.DerivedName));

        var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
        presenter.Save(\"Name\");

        widgetCreator.Verify(a => a.Create(It.IsAny<Widget>()),Times.Once());
    }
}
我很担心,因为如果没有最后的ѭ1调用,就不能保证回调中的assert将被调用。另一种方法是在回调中设置局部变量:
[Test]
public void Properly_Generates_DerivedName()
{
    var widgetCreator = new Mock<IWidgetCreator>();
    Widget localWidget = null;
    widgetCreator.Setup(a => a.Create(It.IsAny<Widget>()))
        .Callback((Widget widget) => localWidget = widget);

    var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
    presenter.Save(\"Name\");

    widgetCreator.Verify(a => a.Create(It.IsAny<Widget>()),Times.Once());
    Assert.IsNotNull(localWidget);
    Assert.AreEqual(\"Derived.Name\",localWidget.DerivedName);
}
我觉得这种方法更容易出错,因为它更明确,而且更容易看到将调用
Assert
语句。一种方法比另一种更好吗?有没有更简单的方法来测试传递给我丢失的模拟的输入参数? 如果有帮助,下面是此示例的其余代码:
public class Widget
{
    public string Name { get; set; }
    public string DerivedName { get; set; }
}

public class WidgetCreatorPresenter
{
    private readonly IWidgetCreator _creator;

    public WidgetCreatorPresenter(IWidgetCreator creator)
    {
        _creator = creator;
    }

    public void Save(string name)
    {
        _creator.Create(
            new Widget { Name = name,DerivedName = GetDerivedName(name) });
    }

    //This is the method I want to test
    private static string GetDerivedName(string name)
    {
        return string.Format(\"Derived.{0}\",name);
    }
}

public interface IWidgetCreator
{
    void Create(Widget widget);
}
编辑 我更新了代码,以使在问题中概述的第二种方法更易于使用。我将在Setup / Verify中使用的表达式的创建拉到一个单独的变量中,因此只需要定义一次即可。我觉得这种方法是我最喜欢的方法,它易于设置,但由于出现错误消息而失败。
[Test]
public void Properly_Generates_DerivedName()
{
    var widgetCreator = new Mock<IWidgetCreator>();
    Widget localWidget = null;

    Expression<Action<IWidgetCreator>> expressionCreate = 
        (w => w.Create(It.IsAny<Widget>()));
    widgetCreator.Setup(expressionCreate)
        .Callback((Widget widget) => localWidget = widget);

    var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
    presenter.Save(\"Name\");

    widgetCreator.Verify(expressionCreate,localWidget.DerivedName);
}

解决方法

由于代码的结构方式,您不得不在一个单元测试中测试两件事。您正在测试A)演示者正在调用注入的WidgetCreator的create方法,以及B)在新的Widget上设置了正确的名称。如果可能的话,最好以某种方式使这两项成为两个单独的测试,但这会更好,但是在这种情况下,我真的看不到这样做的方法。 考虑到所有这些,我认为第二种方法更清洁。它对您的期望更加明确,如果失败,则完全可以理解失败的原因和原因。,我要做的是将ѭ1与AAA相匹配。并且因为此安装程序不是必需的。您可以内联它,但我将其分开以使其看起来更干净。
[Test]
public void Properly_Generates_DerivedName()
{
    var widgetCreator = new Mock<IWidgetCreator>();

    var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
    presenter.Save(\"Name\");

    widgetCreator.Verify(a => a.Create(MatchesWidget(\"Derived.Name\"));
}

private Widget MatchesWidget(string derivedName)
{
    return It.Is<Widget>(m => m.DerivedName == derivedName);
}
,只是要详细说明@rsbarro的评论-Moq失败错误消息: 预期至少对模拟调用一次,但从未执行 当确定确切的
which
条件实际上失败,寻找错误(无论是在代码还是在单元测试中)时,...对于复杂类型没有什么帮助。 在使用Moq
Verify
验证
Verify
中的大量条件时,我经常会遇到这种情况,在该条件中,必须使用特定参数值调用该方法,这些参数值不是诸如
int
string
之类的原语。 (对于原始类型,这通常不是问题,因为
Moq
会在方法中列出实际的“执行的调用”作为异常的一部分)。 结果,在这种情况下,我将需要捕获传入的参数(对我来说,这似乎重复了
Moq
的工作),或者只是将内联声明置为ѭ15with /
Callbacks
。 例如验证:
widgetCreator.Verify(wc => wc.Create(
      It.Is<Widget>(w => w.DerivedName == \"Derived.Name\"
                    && w.SomeOtherCondition == true),It.Is<AnotherParam>(ap => ap.AnotherCondition == true),Times.Exactly(1));
将被重新编码为
widgetCreator.Setup(wc => wc.Create(It.IsAny<Widget>(),It.IsAny<AnotherParam>())
             .Callback<Widget,AnotherParam>(
              (w,ap) =>
                {
                  Assert.AreEqual(\"Derived.Name\",w.DerivedName);
                  Assert.IsTrue(w.SomeOtherCondition);
                  Assert.IsTrue(ap.AnotherCondition,\"Oops\");
                });

// *** Act => invoking the method on the CUT goes here

// Assert + Verify - cater for rsbarro\'s concern that the Callback might not have happened at all
widgetCreator.Verify(wc => wc.Create(It.IsAny<Widget>(),It.Is<AnotherParam>()),Times.Exactly(1));
乍一看,这违反了AAA,因为我们将
Assert
Arrange
内联(尽管仅在
Act
期间调用了回调),但至少可以深入探讨此问题。 另请参见Hady将“ tracking”回调lambda移动到其自己的命名函数中的想法,或者更好的是,在C#7中,可以将其移动到单元测试方法底部的Local Function,因此
AAA
版面可以保留。,在此线程的StuartLC答案的基础上,通过编写传递给模拟对象的“ 1”方法的“内联”函数,可以遵循他的建议而不会违反AAA。 因此,例如:
// Arrange
widgetCreator
  .Setup(wc => wc.Create(It.IsAny<Widget>(),It.IsAny<AnotherParam>());

// Act
// Invoke action under test here...

// Assert
Func<Widget,bool> AssertWidget = request =>
{
  Assert.AreEqual(\"Derived.Name\",w.DerivedName);
  Assert.IsTrue(w.SomeOtherCondition);
  Assert.IsTrue(ap.AnotherCondition,\"Oops\");
  return true;
};

widgetCreator
  .Verify(wc => wc.Create(It.Is<Widget>(w => AssertWidget(w)),Times.Exactly(1));

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...