Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq: how to assert that methods on my mock object are NOT run?

I have mocks working where I test that methods on my mocked object are called with the correct parameters, and return the right result.

Now I want to check another condition. In this case, NO methods should be run on the mocked object. How can I express this in a unit test?

like image 735
David White Avatar asked Jan 29 '09 02:01

David White


1 Answers

You could create your mock as strict. That way only the methods you Setup (or Expect, depending on which version of Moq you're playing with) are allowed to be run.

var foo = new Mock<IFoo>(MockBehavior.Strict);
foo.Expect(f => f.Bar());

Any time a method is called on foo other than Bar(), an exception will be raised and your test will fail.

like image 193
Matt Hamilton Avatar answered Nov 14 '22 21:11

Matt Hamilton