如何验证是否调用了 Microsoft Fakes(测试版)存根/垫片(如 Rhino Mocks 中的 AssertWasCalled)? [英] How can I verify that a Microsoft Fakes (beta) stub/shim was called (like AssertWasCalled in Rhino Mocks)?

查看:37
本文介绍了如何验证是否调用了 Microsoft Fakes(测试版)存根/垫片(如 Rhino Mocks 中的 AssertWasCalled)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 11 中使用 Microsoft Fakes 测试版.如何验证我的被测系统调用了依赖项的方法?

I'm using the beta of Microsoft Fakes in Visual Studio 11. How can I verify that a dependency's method was called by my system under test?

推荐答案

由于 Microsoft Fakes Beta 中不包含验证功能,因此下面的代码是对是否调用依赖项的方法的基本测试.您可以增强 true 测试以测试参数值或正确调用的其他条件.

As verify functionality is not included in the Microsoft Fakes Beta, the code below is a basic test for whether or not a method on a dependency was called. You could enhance the true test to test parameter values or other conditions of a proper call.

测试:

[TestMethod]
public void TestMethod1()
{
    var secondDoItCalled = false;
    var secondStub = new Fakes.ShimSecond();
    secondStub.DoIt = () => { secondDoItCalled = true; };
    var first = new First(secondStub);
    first.DoIt();
    Assert.IsTrue(secondDoItCalled);
}

课程:

public class First
{
    readonly Second _second;
    public First(Second second) { _second = second; }
    public void DoIt() { 
        //_second.DoIt();
    }
}

public class Second {public void DoIt(){}}

取消注释上面的行以查看测试通过.

Uncomment the line above to see the test pass.

这篇关于如何验证是否调用了 Microsoft Fakes(测试版)存根/垫片(如 Rhino Mocks 中的 AssertWasCalled)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆