Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of Verifiable() in Moq?

Tags:

moq

What is the purpose of Verifiable()?

If I verify a Mock and leave this out it still verifies the SetUp.

Edit: I was using VerifyAll() thus the reason for everything being verified. After changing to Verify() only my .Verifiable() SetUps were being checked.

like image 817
Castrohenge Avatar asked Jun 11 '09 11:06

Castrohenge


People also ask

What does verify all do?

VerifyAll() is for verifying that all the expectations have been met.

What is setup in MOQ?

Setup method is used to set expectations on the mock object For example: mock. Setup(foo => foo. DoSomething("ping")). Returns(true);

What is MOQ mocking framework?

Moq is a mocking framework built to facilitate the testing of components with dependencies. As shown earlier, dealing with dependencies could be cumbersome because it requires the creation of test doubles like fakes. Moq makes the creation of fakes redundant by using dynamically generated types.

Is MOQ open source?

The Moq framework is an open source unit testing framework that works very well with .


2 Answers

ADDENDUM: As the other answer states, the purpose of .Verifiable is to enlist a Setup into a set of "deferred Verify(...) calls" which can then be triggered via mock.Verify().

The OP's clarification makes it clear that this was the goal and the only problem was figuring out why it wasn't working, but as @Liam prodded, the answer should really touch on this too:- The key use cases as far as I can see are:

  • maintaining DRYness between a mock.Setup() and mock.Verify
  • allowing one to disconnect the configuring of a verification from the actual Verify call itself (e.g., you could set it up in another helper method)

... and back to my answer, which tersely effectively says "be careful as the above pros are commonly considered to be outweighed by the effect that achieving those goals has on the legibility and maintainability of tests which lean too much on such constructs"

ORIGINAL: Note that where possible, one should instead follow the AAA layout and hence one should be doing explicit mock.Verify( expression ) calls after the work has been done, rather than a mock.Setup( ... ).Verifiable() paired with a mock.Verify() or mock.VerifyAll() wherever possible (credit: @kzu).

like image 123
Ruben Bartelink Avatar answered Oct 20 '22 03:10

Ruben Bartelink


When the Verify() method is called at the end of the test, if any of the expectations marked as verifiable have not been called, then an exception is thrown.

VerifyAll() does not check for verifiable expectations.

like image 24
SuPra Avatar answered Oct 20 '22 04:10

SuPra