【问题标题】:MSpec -> How to verify that a method is calledMSpec -> 如何验证方法是否被调用
【发布时间】:2017-01-12 17:19:51
【问题描述】:

我在我的移动服务应用程序中使用 MSpec。我想验证当传入的参数为空时是否调用了自定义记录器上的方法。这可能吗?

代码

if (someOrg == null || target == null) {
    AppUtils.LogInfo(">>>>> +++ Utils-GetAsNeededItems - Null input");
    return null;
}

【问题讨论】:

    标签: c# testing bdd assertions mspec


    【解决方案1】:

    您可以将Moq 与 MSpec 一起使用。

    // Mock something
    Mock<ISomething> mock = new Mock<ISomething>();
    
    ClassToTest sut = new ClassToTest();
    sut.WorkMethod(mock.Object);
    
    // Make sure the method TheMethodYouWantToCheck was called
    mock.Verify(m => m.TheMethodYouWantToCheck());
    

    您还可以使用 Verify 的重载并确保它被调用一次或至少 x 次,或最多 x 次等。

    mock.Verify(m => m.TheMethodYouWantToCheck(), Times.Once);
    

    【讨论】:

    • 谢谢,有机会我会试试的。
    • FakeItEasy 也是推荐的。它有一个非常干净的 API,并且在功能上与 Moq 相同。
    • 是否可以验证调用了静态记录器?
    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多