How to verify that method was NOT called in Moq?

前端 未结 5 1996
挽巷
挽巷 2020-12-22 16:48

How do I verify that method was NOT called in Moq?

Does it have something like AssertWasNotCalled?

UPDATE: Starting from Version 3.0, a new syntax can be us

5条回答
  •  情书的邮戳
    2020-12-22 17:38

    Stolen from: John Foster's answer to the question, "Need help to understand Moq better"

    One of the things that you might want to test is that the pay method does not get called when a person aged over 65 is passed into the method

    [Test]
    public void Someone_over_65_does_not_pay_a_pension_contribution() {
    
        var mockPensionService = new Mock();
    
        var person = new Person("test", 66);
    
        var calc = new PensionCalculator(mockPensionService.Object);
    
        calc.PayPensionContribution(person);
    
        mockPensionService.Verify(ps => ps.Pay(It.IsAny()), Times.Never);
    }
    

提交回复
热议问题