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

后端 未结 3 486
日久生厌
日久生厌 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条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 15:58

    Another option that you have for doing behavioral verification with the Microsoft Fakes framework is to use the StubObserver class thats included in the Microsoft.QualityTools.Testing.Fakes.Stubs namespace. Using the framework, you generate a stub for your dependency. Then on your Stub you can set the InstanceObserver property to a new StubObserver. Using the StubObserver class, you can "query" the method calls made to your dependency. Your test method would look something like below

    //Arrange
    var dependency = new StubIDependency { InstanceObserver = new StubObserver() };
    var sut = new SystemClass(dependency);
    
    // Act
    sut.DoSomething();
    
    // Assert
    var observer = (StubObserver)dependency.InstanceObserver;      
    Assert.IsTrue(
        observer.GetCalls().Any(call => call.StubbedMethod.Name == "DoSomething"));
    

提交回复
热议问题