Mock object 3: forward! Block Business Objects and mock objects with libraries and references

Source: Internet
Author: User

Introduction

The last time I demonstrated the mock object to the leader, the leader felt very good, but he thought this solution was not perfect because we blocked the Business Objects and mock objects through a base class object, although there is no difference between the twoProgramIn order to create a mock object for testing, we had to refactor the program to achieve this. The leader felt uncomfortable. He wanted a completely transparent solution without modifying any existing row.CodeYou can switch between the business object and the mock object. After thinking about it, it seems that you can import different libraries, that is, IWrite a dynamic link library project, define a class with the same name, method, and attribute as the Business ObjectBecause the Business Object demonstrated last time is actually the system library (system. messaging) the system library is in. net is imported to the project through reference (Visual Studio.. Net project/Application Right-click to add the application in. under the. NET tab, select system. messaging) So we add a messaging Dynamic Link Library Project in our solution, which completely simulates the behavior of the system library, then, we replace the dynamic link library of the system in the project that uses the system library to switch Business Objects and mock objects.

The driver module (main function) of the program that uses the Business Object)

 Using  System;  Using  System. Collections. Generic; Using  System. LINQ;  Using  System. text;  Namespace  Leleapplication1 {  Class  Program {  Static   Void Main ( String  [] ARGs) {queue = New  Queue ();  While ( True  ){  String S = ( String ) Queue. readmessage ( Typeof ( String  ); Console. writeline (s );}}}} 

Modules that use Business Objects

 Using  System;  Using  System. Collections. Generic;  Using  System. LINQ; Using  System. text;  Using  System. messaging;  Namespace  Leleapplication1 {  Class  Queue {messagequeue queue = Null  ;  Public  Queue (){  String Queuename = "  . \ Private $ \ test "  ;  If  (Messagequeue. exists (queuename) {queue = New  Messagequeue (queuename );}  Else  {Queue = Messagequeue. Create (queuename, False  ); Queue. setpermissions (  "  Everyone  " , Messagequeueaccessrights. fullcontrol );}}  Public   Object  Readmessage (type elementtype ){  //  Because elementtype is needed to read the real processing code. System. messaging. Message message = Queue. Receive (); message. formatter = New Xmlmessageformatter ( New  Type [] {elementtype });  Return (Object  ) Message. Body ;}}} 

 

Dynamic Link Library code: Simulate the mock object of the Business Object

 /*  Project name: messaging project type: dynamic link library function: simulates the behavior of system library system. messaging. Other projects can reference this dynamic link library as a system library. This function is mainly used for testing.  */  Using  System;  Using  System. Collections. Generic;  Using  System. LINQ;  Using  System. text; Using  System. Threading;  Namespace  System. messaging {  Public   Enum  Messagequeueaccessrights {fullcontrol = 983103  }  Public   Class  Message {  Public   Object  Body; Public   Object  Formatter ;}  Public   Class  Xmlmessageformatter {  Public  Xmlmessageformatter (type [] targettypenames ){}}  Public   Class  Messagequeue {  Private   Int Elementindex = 0 ;  Private String [] elements = { "  Hello  " , "  World  "  };  Public   Static Messagequeue create ( String Queuename, Bool  Transactional ){  Return  New  Messagequeue ();}  Public   Static   Bool Exists ( String  Queuename ){  Return   True  ;}  Public Messagequeue ( String Queuename = ""  ){} Public   Void Setpermissions ( String  User, messagequeueaccessrights rights ){  //  Do nothing  }  Public  Message receive () {message = New  Message ();  If (Elementindex < Elements. Length) message. Body = Elements [elementindex ++ ];  Else  System. Threading. thread. Sleep (  1000 * 60 * 60  );  Return  Message ;}}} 

End.

Related Article

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.