Reset mock verification in Moq?

前端 未结 8 1651
执笔经年
执笔经年 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:40

    I don't think you can reset a mock like this. Instead, if you know that Fizz should be called once when transitioning to state 1, you can do your verifies like this:

    objectUnderTest.DoStuffToPushIntoState1();
    foo.Verify(x => x.Fizz(), Times.Once());  // or however many times you expect it to be called
    
    objectUnderTest.DoStuffToPushIntoState2();
    foo.Verify(x => x.Fizz(), Times.Once());
    

    Having said that, I would still create two separate tests for this. As two tests, it's easier to see whether the transition into state 1 is failing, or the transition into state 2 is failing. Additionally, when tested together like this, if your transition into state 1 fails, the test method exits and your transition into state 2 doesn't get tested.

    Edit

    As an example of this, I tested the following code with xUnit:

    [Fact]
    public void Test()
    {
        var foo = new Mock(MockBehavior.Loose);
    
        foo.Object.Fizz();
        foo.Verify(x => x.Fizz(), Times.Once(), "Failed After State 1");
    
        // stuff here
        foo.Object.Fizz();
        foo.Verify(x => x.Fizz(), Times.Once(), "Failed after State 2"); 
    }
    

    This test fails with the message, "Failed after State 2". This simulates what would happen if your method that pushes foo into State 2 calls Fizz. If it does, the second Verify will fail.

    Looking at your code again, since you are calling one method to verify it does/does not call another method on the mock, I think you need to set CallBase to true so that the base DoStuffToPushIntoState2 is called rather than the mock's override.

提交回复
热议问题