Reset mock verification in Moq?

前端 未结 8 1883
执笔经年
执笔经年 2021-02-11 12:01

Setup as so:

public interface IFoo
{
    void Fizz();
}

[Test]
public void A()
{
    var foo = new Mock(MockBehavior.Loose);

    foo.Object.Fizz();         


        
8条回答
  •  离开以前
    2021-02-11 12:46

    I think long after this post was created they added the functionality that the OP had asked for, there is a Moq extension method called Moq.MockExtensions.ResetCalls().

    With this method you can do exactly what you wished as shown below:

    [Test]
    public void Justification()
    {
        var foo = new Mock(MockBehavior.Loose);
        foo.Setup(x => x.Fizz());
    
        var objectUnderTest = new ObjectUnderTest(foo.Object);
    
        objectUnderTest.DoStuffToPushIntoState1(); // this is various lines of code and setup
    
        foo.Verify(x => x.Fizz());
    
        foo.ResetCalls(); // *** Reset the verification here with this glorious method ***
    
        objectUnderTest.DoStuffToPushIntoState2(); // more lines of code
    
        foo.Verify(x => x.Fizz(), Times.Never());
    }
    

    Update

    Now instead of .ResetCalls() we should use .Invocations.Clear() on the latest version of the library:

    foo.Invocations.Clear()
    

提交回复
热议问题