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.