How to verify that method was NOT called in Moq?

前端 未结 5 1994
挽巷
挽巷 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:30

    This does not work in recent versions of Moq (since at least 3.1), it should be specified in the Verify method as mentioned in the answer.

    Actually, it's better to specify .AtMost(0) after the Returns statement.

    var m = new Mock();
    m.Expect(x => x.Forbidden()).Returns("foo").AtMost(0);
    

    Although the "throws" also works, AtMost(0) is more expressive IMHO.

提交回复
热议问题