HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How to verify that method was NOT called in Moq?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
moqhowcalledverifymethodnotthatwas

Problem

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());

Solution

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."));

Code Snippets

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

Context

Stack Overflow Q#537308, score: 171

Revisions (0)

No revisions yet.