I have started using moq for mocking. Can someone explain me the concept of strict and non-strict mocks? How can they can be used in moq?
edit:
in which scenario do we use which type of mock?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m not sure about moq specifically, but here’s how strict mocks work in Rhino. I declare that I expect a call to
foo.Baron my objectfoo:If the calling code does
then I’m fine because the expectations are exactly met.
However, if the calling code is:
then my expectation failed because I did not explicitly expect a call to
foo.Quux.To summarize, a strict mock will fail immediately if anything differs from the expectations. On the other hand, a non-strict mock (or a stub) will gladly “ignore” the call to
foo.Quuxand it should return adefault(T)for the return typeToffoo.Quux.The creator of Rhino recommends that you avoid strict mocks (and prefer stubs) because you generally don’t want your test to fail when receiving an unexpected call as above. It makes refactoring your code much more difficult when you have to fix dozens of test that relied on the exact original behavior.