How can I verify that a Microsoft Fakes (beta) stub/shim was called (like AssertWasCalled in Rhino Mocks)?

后端 未结 3 480
日久生厌
日久生厌 2020-12-20 15:22

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?

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 16:12

    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.

    Test:

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

    Classes:

    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.

提交回复
热议问题