0

I've just started looking into the moq framework and a bit confused. I've seen people use setup to layout the expected behavior, take a simple example where you are just checking that a function was called. Then when asserting i've seen verifyall called.

But I've also seen people not using setup and passing in a lambda to verify to check that the function was called. When would you use setup over verify and vice versa?

user48408
  • 3,234
  • 11
  • 39
  • 59
  • An 'older' style of testing involved setting up Mocks and the expectations of how they would be interacted with at the same time, before the call to the system under test. This has subsequently been evolved into the more modern "Arrange, Act, Assert" style, where the Verifies are done as explicit steps after the `Act`, at the same time as any asserts. So `VerifyAll()` or empty `Verify()` should be avoided in favour of explicit `.Verify(() => ...)`. [More here](http://stackoverflow.com/q/980554/314291) – StuartLC Jul 07 '14 at 13:53

1 Answers1

0

.Setup followed by .Verify is a shortcut form of saying you're setting up a method and then want to verify that it was called. When to use it over only .Verify depends on the style and standards. But there might be some scenario where you're setting up a method in the TestInitialize method for all the tests and not all tests want to verify, in that case only some unit tests might do .Verify only.

Also, I personally use .Verify when I want to test the parameters, it seems a bit easier to read for me.

AD.Net
  • 13,352
  • 2
  • 28
  • 47