I'm a fresh man in VS Unit Test, and I'm learning to add mock module into my unit test project with the SampleStub
Framework.
And I now meet the trouble in understanding MockBehavior.Loose
and MockBehavior.Strict
. What I truly wanna know is the difference between these two choose ? Can this choose makes a great infect to my unit test?
Strict behavior means that exceptions will be thrown if anything that was not set up on our interface is called. Loose behavior, on the other hand, does not throw exceptions in situations like this. Mocks, by default, are loose.
Normal (or loose) mocks are used when you want to verify that an expected method has been called with the proper parameters. Strict mocks are used to verify that only the expected methods have been called and no other.
Mock behaviorMoq's default behavior enables quicker development of unit tests by defining a fallback configuration that accepts any parameter and returns empty sequences or the default value. Sometimes there is a need for a more controlled environment by requiring Moq to attain a more strict behavior.
If you're using MockBehaviour.Strict
the mock behaves just like the object of the class you've mocked. It causes the mock to always throw an exception for invocations that don't have a corresponding expectation.
Thus, if the you slightly changed the class (added a method), you'll also want to add that method to the mock to make your tests pass.
MockBehavior.Loose
on the other hand will never throw exceptions, returning default values when necessary (null for reference types or zero for value types). This is the default behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With