Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between MockBehavior.Loose and MockBehavior.Strict in SimpleStub?

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?

like image 999
Chopping Avatar asked May 07 '18 09:05

Chopping


People also ask

What is the difference between strict and loose mocking Behaviour?

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.

When you need to use mocks in unit tests What is the difference between strict and loose mock?

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.

What is mock Behaviour?

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.


1 Answers

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.

like image 73
Igor Drozdov Avatar answered Oct 17 '22 00:10

Igor Drozdov