The first article, introduction to the concept of mock: https://www.cnblogs.com/cgzl/p/9294431.html
The second article, Introduction to method mock: https://www.cnblogs.com/cgzl/p/9300356.html
The third article, on the description of attribute mock: https://www.cnblogs.com/cgzl/p/9304567.html
The third article, Introduction to behavioral mock: https://www.cnblogs.com/cgzl/p/9306728.html
This article describes some other features that you use MOQ.
Code used: Https://github.com/solenovex/Moq4-Tutorial-Code inside the Before section.
Throw exception
This refers to having the mock object throw an exception.
First create a test method and set the player to be under the age of 16, Iphysicalexamination will throw an exception when calling the Ishealthy () method:
Then in the player approval method, you need to catch this exception and return the corresponding result:
This test will pass:
The above uses a generic version to throw an exception, or you can throw an exception directly without using generics:
Events
We can also allow mock objects to cause events.
First add an event to Iphysicalexamination:
This event should be caused whenever the Ishealthy () method is called.
In the implementation class, you also need to add this event, but temporarily do not do any other action on it:
In the transfer approval category, make these changes:
Add a method to handle the event, and if the event is triggered, then the playerhealthchecked will become true.
Create a test method to determine whether the event is triggered:
This test should fail:
Because the event was not triggered (no action was made in physicalexamination).
At this point, we can use the mock object to trigger the event, and in the test method, manually invoke the Raise () method of the Mock object:
The first parameter is a lambda expression, which is bound to null, and the second argument is eventargs.empty for this example.
After testing, you will pass:
The second method is to set the event trigger when setting the Ishealthy () method:
In this case, if the Ishealthy () method is called, the Healthchecked event is also triggered.
The test will also pass:
Set different return values for successive calls
Use the setupsequence () method to return a specified value (or throw a specified exception) each time a member of a mock object returns a continuous invocation:
When the test executes, the first time the Ishealthy () method is called, the method returns True, and the checkup passes; When the second call to the Ishealthy () method returns false, the medical examination does not pass.
This test is going to pass:
Mock methods that do not implement an interface
First, create a class that does not implement any interfaces, and there is a way to determine whether the current is in the transfer period:
Then use this class in the Transfer approval class:
Create a test method that mocks the class above:
But the test fails back:
This is because MOQ requires that the method be virtual in the mock of this kind.
Then modify the method to virtual:
In this case, the test will pass:
Protected Virtual method
Add a protected virtual method to the above class:
If you want to mock this method, you need to use this:
If you have parameters, you need to use the itexpr class.
Linq to Mocks
We can write the previously written setup code using LINQ:
Here you use the mock.of<t> () method. The lambda expression of the parameter allows you to set the return value of the member of the mock object.
Because this method returns the desired type directly, it is not necessary to use the mock object. The object property.
The test will also pass:
Moq's introduction to here, in fact, look at official documents will be more rapid: Https://github.com/Moq/moq4/wiki/Quickstart
After the refactoring code in: Https://github.com/solenovex/Moq4-Tutorial-Code inside.
Test. NET Core applications using MOQ-Other