Test the legacy code

Source: Internet
Author: User

Test the legacy code

In most cases, it is difficult to test the code. Because many codes cannot inject parameters, there is an unrestricted isolation framework TypeMock for you to use, unfortunately, this software is a paid isolation framework and has a 15-day free use right. If you can solve your existing problems, I don't think the cost is too much. Official download of TypeMock: http://www.typemock.com /. Next let's take a look at how TypeMock is used.

1: forge a static method. Let's look at an example.

Static Method tested

Public static int DoSomethingSpecialOnALeapYear () {if (DateTime. Now. Month = 3) & (DateTime. Now. Day = 29) return 100; return 0 ;}DoSomethingSpecialOnALeapYear

Test Method

1 [TestMethod, Isolated] 2 public void FakingDateTime () 3 {4 Isolate. whenCalled () => DateTime. now ). willReturn (new DateTime (2016, 3, 29); // when the current call time is returned, the specified time is 5 int result = MyStatic. doSomethingSpecialOnALeapYear (); 6 Assert. areEqual (100, result); 7}FakingDateTime

2: forge a sealed class

Static Method tested

1 public static bool SignedIn {get; set;} 2 3 public static bool SignOut (HttpContext current) 4 {5 if (SignedIn) 6 {7 HttpSessionState session = current. session; 8 session. abandon (); 9 return true; 10} 11 return false; 12}SignOut

If we want to ensure that the above method does not depend on HttpContext, We must inject this dependency. Let's look at the test code.

1 [TestMethod, Isolated] 2 public void FakingHttpContext () 3 {4 var fakeHttp = Isolate. fake. instance <HttpContext> (); // pseudo object to be injected 5 MyStatic. signedIn = true; 6 var result = MyStatic. signOut (fakeHttp); 7 Assert. areEqual (true, result); 8}FakingHttpContext

3: override methods in the counterfeit class

If some classes need to return the corresponding values as needed, let's take a look at the example below.

1 public static bool IsMySiteNameTypemock (Process process) 2 {3 var name = process. machineName; 4 if (process. mainModule. site. name. startsWith ("Typemock") 5 return true; 6 else7 return false; 8}IsMySiteNameTypemock

In order for the above program to work normally, we must use site. Name to start with Typemock, so we must do this in the test.

1 [TestMethod, Isolated] 2 public void SetBehaviorOnChainExample () 3 {4 var fake = Isolate. fake. instance <Process> (); 5 Isolate. whenCalled () => fake. mainModule. site. name ). willReturn ("Typemock rocks"); // sets the Site. name6 var result = MyStatic. isMySiteNameTypemock (fake); 7 Assert. areEqual (true, result); 8}SetBehaviorOnChainExample

Now let's run the test to see the result.

4: skip an exception

If you want to execute a method (a dependency) in a method body and then follow the code below, let's look at this example.

1 public class Dependency // a Dependency Item 2 {3 public virtual void CheckSecurity (string name, string password) 4 {5 throw new SecurityException (); 6} 7}Dependency1 public int Calculate (int a, int B, Dependency dependency) 2 {3 dependency. CheckSecurity ("typemock", "rules"); 4 return a + B; 5}Calculate

Next, we need to skip the CheckSecurity method, which will report exceptions. Let's take a look at the example.

1 [TestMethod, Isolated] 2 public void FakeMethod_OnLiveObject () {3 var dependency = new Dependency (); 4 Isolate. whenCalled () => dependency. checkSecurity (null, null )). ignoreCall (); // call this method without considering 5 var classUnderTest = new ClassUnderTest (); 6 var result = classUnderTest. calculate (1, 2, dependency); 7 Assert. areEqual (3, result); 8}FakeMethod_OnLiveObject

5. Create a pseudo object in a future method body

The analogy is that in a put Method A, we will depend on A Class B. So what should we do if this is instantiated internally. Let's look at the example below.

The external Dependency is still the Dependency above.

Now let's modify the Calculate method as follows:

1 public static int Calculate (int a, int B) {2 var dependency = new Dependency (); 3 dependency. checkSecurity ("typemock", "rules"); 4 return a + B; 5}Calculate

The Dependency object is instantiated directly in Calculator.

Let's see how to inject this pseudo object.

1 [TestMethod, Isolated] 2 public void FakeConstructor () {3 var fakeHandle = Isolate. fake. nextInstance <Dependency> (); // you do not need to inject a pseudo object created in the future. 4 var result = ClassUnderTest. calculate (1, 2); 5 Assert. areEqual (3, result); 6}FakeConstructor

We use NextInstance to create this pseudo object in a future method body. OK. Let's take a look at the test results.

The above briefly introduces the basic usage of a typemock.

 

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.