验证传递给Mock的参数的正确方式按预期设置

本文关键字:方式按 设置 参数 Mock 验证 | 更新日期: 2023-09-27 17:50:35

如果您稍后验证方法是否被调用,那么在回调中执行断言是否可以接受?这是确保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());
    }
}

我很担心,因为如果最后没有Verify调用,就不能保证回调中的断言会被调用。另一种方法是在回调中设置一个局部变量:

[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语句将被调用。一种方法比另一种更可取吗?有没有一种更简单的方法来测试传递给我缺少的mock的输入参数?

如果有用的话,下面是这个例子的其余代码:

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, Times.Once());
    Assert.IsNotNull(localWidget);
    Assert.AreEqual("Derived.Name", localWidget.DerivedName);
}

验证传递给Mock的参数的正确方式按预期设置

我所做的是使用与AAA保持匹配的Verify。因此,不需要安装程序。你可以把它排成一行,但我把它分开是为了让它看起来更干净。

[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);
}

由于代码的结构方式,您被迫在一个单元测试中测试两件事。您正在测试A(您的演示者正在调用注入的WidgetCreator的创建方法,以及B(在新的Widget上设置了正确的名称。如果可能的话,如果你能以某种方式将这两件事作为两个单独的测试,那会更好,但在这种情况下,我真的没有办法做到这一点。

考虑到这一切,我认为第二种方法更干净。它更明确地表明了你的期望,如果它失败了,那么它为什么失败以及在哪里失败是完全合理的。

只需详细说明@rsbarro的评论-Moq失败错误消息:

预期对mock至少调用一次,但从未执行

对于复杂类型,当确定which条件实际失败时,当查找错误时(无论是在代码还是单元测试中(,都没有什么帮助。

当使用Moq Verify来验证Verify中的大量条件时,我经常遇到这种情况,其中该方法必须使用特定的参数值来调用,这些参数值不是像intstring这样的基元。(对于基元类型来说,这通常不是问题,因为Moq将方法上实际"执行的调用"列为异常的一部分(。

因此,在这种情况下,我需要捕获传入的参数(对我来说,这似乎与Moq的工作重复(,或者只是将断言与Setup/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,因为我们将AssertArrange内联(尽管回调在Act期间仅调用(,但至少我们可以弄清问题的根源。

另请参阅Hady将"跟踪"回调lambda移动到其自己的命名函数中的想法,或者更好的是,在C#7中,可以将其移动到单元测试方法底部的局部函数中,这样就可以保留AAA布局。

在这个线程中,在StuartLC的答案的基础上,您可以按照他的建议,在不违反AAA的情况下,编写一个"内联"函数,该函数传递给mock对象的Verify方法。

例如:

// 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)), It.Is<AnotherParam>()), Times.Exactly(1));