Test. NET Core applications using MOQ-Mock behavior

Source: Internet
Author: User

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

This article describes the behavior test using MOQ.

Code used: Https://github.com/solenovex/Moq4-Tutorial-Code inside the Before section.

Mock behavior

The first two articles are all state- based testing , and after the code is executed, we insist that the state of the tested system is consistent with what we expect.

This article describes behavioral testing , which means that we want to make sure that certain methods are executed or some properties are accessed.

Confirmation method is called

To create a new test method:

Unlike the status test, where I don't use assert, I'm using a mock. Verify () to determine how the method in its parameters will be executed. You can also use the It class to match parameters here.

The test will pass, indicating that the ishealthy is actually executed:

If I change the verify. The parameter range of the Ishealthy () method:

Then for this example, the test fails:

This is because the parameters of the Ishealthy () method when called are inconsistent with the parameters I expect.

Custom error messages

At this point, we can use another overloaded version of the Verify () method, which can add an error message to the parameter:

At this point the test fails with the following information:

In addition, by overloading the method, you can also determine how many times the method has been called

The number of times the method was called

The heavy-duty method of verify and the Times of the struct are still used.

1. Not called:

2. Called 1 times:

3. Called a specific number of times:

There are many options in this struct, please feel free to explore it yourself.

Confirm Property Access

The first is get.

Unlike the previously called Verify (), the Verifyget () method is required for attributes to ensure that the property is accessed:

This test will pass because this property is actually accessed:

Check for another property that is not being accessed:

Then the result will fail:

The following is set.

Very simple, use the Verifyset () method. Then create a test method:

In the Verifyset method, you need to set the Set property and the value of the set.

For this example, this test will pass:

However, if the set value is incorrect, the test fails:

This article first come to this .... Not to be continued ....

Code: Https://github.com/solenovex/Moq4-Tutorial-Code.

Test. NET Core applications using MOQ-Mock behavior

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.