Moles-isolation framework for. Net (false. Net)

Source: Internet
Author: User

Moles is the mock framework used in the previous article 91. It is similar to Moq and rhino. mocks and rhino are different. mocks mock is a mock for the interface or abstract class, while moles is the Mock's entire CLR (Common Language Runtime), breaking Moq and other mock suite restrictions, non-interface or abstract class can also be mock, and static method can also be mock, such as datetime. now or file. open and other static methods, so that they didn't have to worry about making it easier for users to design the design during development (for example, before the younger brother, add another datetime datetimeprovider. When I see this framework, I think my previous practice is very boring. You can take the unit test tips: datetime's Stub ).

 

• What is mock?

Mock is just a fake thing. It is usually used in a single-dollar transaction because when a single-dollar transaction occurs, the credit card API can be used to call different methods, such as ticket handling, you can use mock to convert your credit card API into a fake credit card API, so that you can renew your credit card on a billing basis, the usage and concept of mock are not the same. You can take the test on your situation:

  • ASP. net mvc single-dollar zookeeper series by Will Bao Ge
  • In 91 serial numbers

Note:

Mock and stub are both used to cheat objects, but they are actually poor. Simply put, mock is produced by Action (such as Moq ), stub is already good (for example, it is called a mock in this article). Although moles feels better than stub.

 

• Comparison Between Moq and moles

Moq's mock principle is to generate a class, so it can only interface or abstract class. This type can be passed by others, for example, an interface is a class that generates a real interface for all Members. When setup is used to specify the content or response value of the member, most of them will be used with IOC, let the program call other types of calls to the Mock program.

Note:

Most mock frameworks such as Moq and rhino mocks use Castle dynamicproxy to generate classes.

 

Moles's mock principle is to set up its own Runtime on CLR. during runtime, the line is written on moleruntime, so it was originally called directly, such as datetime. now, change to the Mock call program.

Assume a hypothetical role of moleruntime

 

Moq and moles both have combined use time and restrictions. You can take the second test.

Second, the difference between mole and general mock is used as the source: Microsoft moles Reference Manual

 

• Security moles

Moles

Visual Studio 2010 moles-isolation framework for. Net (x86)

Visual Studio 2010 moles-isolation framework for. Net (x64)

 

Pex and moles

Pex-automated whitebox testing for. Net (x86)

X64 is currently only available inMsdn suscribersLower and lower layers

Note:

Pex is a code analysis tool that can generate related single-unit metadata. It is developed in the same region. They integrate two sets of frameworks, individual users use the results, which are interesting but not highly practical.

 

• Implement mock of datetime. Now

1. added the zookeeper case.

 

2. Select "add moles assembly for mscorlib" and generate mscorlib.

Added mscorlib. Moles Plugin in Release 3.

 

When there is a. Moles attack in the case, some organizations will generate their corresponding components.

Compatible with mscorlib. Moles Components

Note:

In the domestic version of moles, mscorlib. Moles plugin is added inNew categoryWhen you manually upload the component name, the new version directly adds *. moles to the right of the examination, which is much more convenient.

In November 5, moles has canceled the method of adding a new primary node because it cannot find the primary node used by Cheng xiangda.

 

MscorlibIs. NET Framework is the most basic functionBase Class Library (BCL)That is to say, all the tests in the case are excluded, and the remaining available models are inMscorlib. dllDatetime is also one of them, while all typesMscorlib. dllMust be the same, suchWebapplicationAndSilverlightOfMscorlib. dllYes. (this is the case I did not remember. If this is the case, please see it ).

 

If you want to add a mock other component on another component, press the right button to add the. Moles component of the component.

6th add other moles Components

 

3. In testmethod, the author wrote "invalid datetime" and "mock datetime" and "verification.

[TestMethod][HostType("Moles")]public void TestMethod1(){    System.Moles.MDateTime.NowGet = () => { return new DateTime(2010, 1, 1); };    Assert.AreEqual(new DateTime(2010, 1, 1), DateTime.Now);}

 

Add [hosttype ("moles")] on testmethod to let Visual Studio know that this testmethod is to use moleruntime to parse rows, mock is to use delegate, for example, four moles will be created.MscorlibMock assembly, the memory usage is as follows:

  • Namespace: Add moles
  • Type: Add m in front
  • Property: add get or set to the backend.
  • Method
  • Instance: operate under allinstances

Therefore, the mock of system. datetime. Now is in system. Moles. mdatetime. Nowget. In the examples, datetime. Now is changed to the Mock value instead of the current time.

 

4. added the moledtype declaration.

[assembly: Microsoft.Moles.Framework.MoledType(typeof(DateTime))]

At the top of the caseAssemblyinfo. CSAdd the moledtype declaration to each mock type.

 

5. Complete the rows and pass them.

 

• Known issues

I'm using the moles version.0.94.51023.0When moleruntime could not be used to obtain the response example app. config, I also asked others in msdn. The warning information I learned is a bug. You can refer to can't get data via configurationmanager. appsetaskon hosttype ("moles"), but there are no methods for solving problems in the mountains. moleruntime will only capture your own apps. as long as the data is stored in the moleruntime app. when the configuration is completed, you can obtain the data normally. However, you cannot configure xcopy easily, and all cases share an app. config. In the end, I only want to change the case when I switch the case.

7. App. config

Moles's parallel rows are used in

C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ common7 \ ide \ privateassemblies \ microsoft.moles.vshost.x86.exe

 

• Other examples
// Only a fixed set of guidsystem is used for temporary upload. moles. mguid. newguid = () => {return New GUID ("FFEB1621-5016-4F28-94AE-5DB72FC8A23B") ;}; // when loading, you do not need to actually compile the system. io. moles. mfile. readalltextstring = (filepath) =>{ return "internal volume content" ;}; system. net. mail. moles. msmtpclient. allinstances. sendmailmessage = (SMTP, message) =>{// when sending an email, it is not actually sent };

 

• Additional information

Moles-isolation framework for. net

Cheng Xiang-Yuan (7): Using moles to increase scalability

Moq

Rhino. mocks

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.