How to verify that method was NOT called in Moq?

alex picture alex · Feb 11, 2009 · Viewed 77.5k times · Source

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 used:

mock.Verify(foo => foo.Execute("ping"), Times.Never());

Answer

Dan picture Dan · Aug 5, 2010

Run a verify after the test which has a Times.Never enum set. e.g.

_mock.Object.DoSomething()
_mock.Verify(service => service.ShouldntBeCalled(), Times.Never);