How to verify that method was NOT called in Moq?

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

    UPDATE: Since version 3, check the update to the question above or Dann's answer below.

    Either, make your mock strict so it will fail if you call a method for which you don't have an expect

    new Mock(MockBehavior.Strict)
    

    Or, if you want your mock to be loose, use the .Throws( Exception )

    var m = new Mock(MockBehavior.Loose);
    m.Expect(a => a.moo()).Throws(new Exception("Shouldn't be called."));
    

提交回复
热议问题