Mock object introduction and simple implementation

Source: Internet
Author: User

Recently I have been studying TDD. When testing-driven development, I often use Mock objects when testing modules depend on other modules, so I would like to share my recent learning experiences with you. I hope you can tell me something wrong!

We all know that unit testing is the smallest unit to check program correctness. In object-oriented development, we often test a method to verify whether the method is correct. That is to say, if this method is wrong, I hope this method is wrong, not the method on which it depends. If there are 10 errors when the unit test runs, we hope that there are indeed 10 errors. Therefore, the independence of unit tests is very important, but unit tests often rely on other methods. Even if we try our best to decouple them and abstract an interface for the methods we depend on, although methods depend on abstraction at this time, we must provide implementation. If we can provide a correct implementation by ourselves, to ensure that the method to be tested depends on a stable and correct implementation, we can eliminate a disturbance that affects the correctness of the test. However, this is only one scenario where Mock objects are used.

We often use Mock objects in these scenarios:

1. the objects we depend on are unstable. (Changes often make our unit tests unstable)

2. Dependent objects are difficult to create (maybe this object has not been developed by colleagues in other groups)

3. The access speed of the dependent object is very slow (the dependent object may be connected to a remote database, but the access speed is very slow. As we all know, the basic principle of unit testing is to implement it quickly, which is obviously against this)

Well, as I mentioned earlier, it is a common method to abstract an interface for the dependent modules. But do we need to create a class every time and implement this interface by ourselves? Of course not. There are many Mock object frameworks that can help me dynamically create an implementation. Here I will briefly introduce one. in the Moq framework under the NET platform, my focus is to provide a simple implementation by imitating the Moq framework API, but the most important thing is to give some ideas to friends who are interested in the Mock object framework. Full text reading

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.