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

时间:2011-04-18 21:20:35

标签: c# unit-testing moq

如果稍后验证方法被调用,在回调中执行断言是否可以接受?这是确保我的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语句。一种方法比另一种更好吗?有没有更简单的方法来测试传递给我缺少的模拟的输入参数?

如果有用,以下是此示例的其余代码:

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

4 个答案:

答案 0 :(得分:4)

我所做的是使用符合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);
}

答案 1 :(得分:3)

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

考虑到这一切,我认为第二种方法更清洁。它更明确地表达了你所期待的东西,如果它失败了,它就会完全理解为什么以及失败的地方。

答案 2 :(得分:3)

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

  

模拟上的预期调用至少一次,但从未执行过

...在确定which条件确实失败时,在搜索错误时(无论是在代码还是单元测试中),对复杂类型的帮助不大。

我在使用Moq Verify验证Verify中的大量条件时经常会遇到这种情况,其中必须使用特定参数值调用方法,这些参数值不是int等基元}或string。 (对于基本类型,这通常不是问题,因为Moq列出了作为异常一部分的方法上的实际“已执行调用”。

因此,在这种情况下,我需要捕获传入的参数(这对我来说似乎复制了Moq的工作),或者只是将Assertion与Setup /内联移动Callbacks

e.g。验证:

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内联(尽管在{期间回调只是调用) {1}}),但至少我们可以找到问题的根源。

另请参阅Hady关于将“跟踪”回调lambda移动到其自己的命名函数中的想法,或者更好的是,在C#7中,这可以移动到单元测试方法底部的Local Function,因此可以保留Act布局。

答案 3 :(得分:1)

在此帖子的StuartLC回答的基础上,通过编写传递给Verify方法的“内联”函数,您可以按照他的建议行事而不违反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)), It.Is<AnotherParam>()), Times.Exactly(1));
相关问题