Use Microsoft fakes isolation test code

Source: Internet
Author: User

One of the problems we encountered in unit test is: if the tested component (class or project) is a, component A depends on Component B, therefore, when testing a in unit test atest of component A, it also depends on B. After B is changed, the test result of A may be affected. To isolate the impact of B on a during test A, this is the problem Microsoft fakes should solve.

To solve the problem above, Microsoft fakes replaces component B with stubs or shims.

See: http://msdn.microsoft.com/library/vstudio/hh549175

Stub uses the following method: Replace the dependent class with a class that implements the same interface. The premise for using this method is that a depends on B, which must be an "interface" instead of a specific class, so that it is possible to create a stub that implements interface B.

Shim adopts the following methods:

So in general, use stubs for cballs within your Visual Studio solution, and Shims for cballs to other referenced assemblies.

Use stubs

See: http://msdn.microsoft.com/en-us/library/vstudio/hh549174

The main points of this article are:

You need to add a reference to a in atest -- atest is used to test a. Of course, you need to add a reference to;

You need to add the B reference on which a depends in atest -- this is because you need to create stubs for B.

At the same time, B may also depend on other components. You also need to add references to other components in atest; otherwise, the atest still cannot run normally.

Right-click B's reference and choose add fakes assembly"

In the test code, create a stub instance and provide implementation methods.

Use Microsoft fakes isolation test code

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.