Fakes Visual Studio 2012 - How to check if a method is called?

Based on the use of Moq, I'm used to being able to set Mocks as Confirmed. As you know, this is convenient if you want your test code to be actually called the dependency method.

eg. in Moq:

// Set up the Moq mock to be verified
mockDependency.Setup(x => x.SomethingImportantToKnow()).Verifiable("Darn, this did not get called.");
target = new ClassUnderTest(mockDependency);
// Act on the object under test, using the mock dependency
target.DoThingsThatShouldUseTheDependency();
// Verify the mock was called.
mockDependency.Verify();

I use the VS2012 "Fakes Framework" (due to the lack of a better name for it), which is pretty smooth, and I'm starting to prefer Moq because it seems a little more expressive and makes Shims easy. However, I cannot figure out how to reproduce behavior similar to the Moq Verifiable / Verify implementation. I found the InstanceObserver property in Stubs, which is similar to what I want, but there is no documentation as of 9/4/12, and I don’t understand how to use it, even if it is correct.

- - Moq Verifiable/Verify VS2012?

- 9/5/12 - , , VS2012 Fakes. , - , . , (, ).

[TestClass]
public class ClassUnderTestTests
{
    private class Arrangements
    {
        public ClassUnderTest Target;
        public bool SomethingImportantToKnowWasCalled = false;  // Create a flag!
        public Arrangements()
        {
            var mockDependency = new Fakes.StubIDependency  // Fakes sweetness.
            {
                SomethingImportantToKnow = () => { SomethingImportantToKnowWasCalled = true; }  // Set the flag!
            }
            Target = new ClassUnderTest(mockDependency);
        }
    }

    [TestMethod]
    public void DoThingThatShouldUseTheDependency_Condition_Result()
    {
        // arrange
        var arrangements = new Arrangements();
        // act
        arrangements.Target.DoThingThatShouldUseTheDependency();
        // assert
        Assert.IsTrue(arrangements.SomethingImportantToKnowWasCalled);  // Voila!
    }
}

- 9/5/12 End edit -

!

+5
2

, (Arrangements) . , Fakes, . , , ClassUnderTest IDependency.

[TestMethod]
public void DoThingThatShouldUseTheDependency_Condition_Result()
{
    // arrange
    bool dependencyCalled = false;
    var dependency = new Fakes.StubIDependency()
    {
        DoStuff = () => dependencyCalled = true;
    }
    var target = new ClassUnderTest(dependency);

    // act
    target.DoStuff();

    // assert
    Assert.IsTrue(dependencyCalled);
}
0

All Articles