其实很多时候,重构真的能够帮助我们找到出口,好比俗话说的:"出口在转角处“,只有不断重构才能够逐渐的保证项目的质量,而这种效果是很难得的。
  提取抽象基类,对测试要访问的环境进行明确的定义。
1 namespace OrderManager.Test
2 {
3     public abstract class ProductServiceIntegrationBase
4     {
5         /// <summary>
6         /// service address.
7         /// </summary>
8         protected const string ServiceAddressForDev = "http://dev.service.ProductService/";
9
10         /// <summary>
11         /// service address.
12         /// </summary>
13         protected const string ServiceAddressForPrd = "http://Prd.service.ProductService/";
14
15         /// <summary>
16         /// service address.
17         /// </summary>
18         protected const string ServiceAddressTest = "http://Test.service.ProductService/";
19     }
20 }
  对具体的测试类消除重复代码,加入统一的构造方法。
1 using System;
2 using Microsoft.VisualStudio.TestTools.UnitTesting;
3
4 namespace OrderManager.Test
5 {
6     using ProductService.Contract;
7
8     /// <summary>
9     /// Product service integration tests.
10     /// </summary>
11     [TestClass]
12     public class ProductServiceIntegrationTest : ProductServiceIntegrationBase
13     {
14         /// <summary>
15         /// product service client.
16         /// </summary>
17         private ProductServiceClient serviceInstance;
18
19         /// <summary>
20         /// Initialization test instance.
21         /// </summary>
22         [TestInitialize]
23         public void InitTestInstance()
24         {
25             serviceInstance = ProductServiceClient.CreateClient(ServiceAddressForDev/*for dev*/);
26         }
27
28         /// <summary>
29         /// Product service get product by pid test.
30         /// </summary>
31         [TestMethod]
32         public void ProductService_GetProductByPid_Test()
33         {
34             var testResult = serviceInstance.GetProductByPid(0393844);
35
36             Assert.AreNotEqual(testResult, null);
37             Assert.AreEqual(testResult.Pid, 0393844);
38         }
39
40         /// <summary>
41         /// Product service delete search index test.
42         /// </summary>
43         [TestMethod]
44         public void ProductService_DeleteProductSearchIndex_Test()
45         {
46             var testResult = serviceInstance.DeleteProductSearchIndex();
47
48             Assert.IsTrue(testResult);
49         }
50     }
51 }
  消除重复代码后,我们需要加入对具体测试用例检查是否能够连接到某个环境中去。我加入了一个DeleteProductSearchIndex测试用例,该用例是用来测试删除搜索索引的,这个测试用例只能够在本地DEV环境中运行(你可能觉得这个删除接口不应该放在这个服务里,这里只是举一个例子,无需纠结)。
  为了能够有一个检查机制能提醒开发人员你目前连接的地址是哪一个,我们需要借助于测试上下文。
  重构后,我们看一下现在的测试代码结构。
1 using System;
2 using Microsoft.VisualStudio.TestTools.UnitTesting;
3
4 namespace OrderManager.Test
5 {
6     using ProductService.Contract;
7
8     /// <summary>
9     /// Product service integration tests.
10     /// </summary>
11     [TestClass]
12     public class ProductServiceIntegrationTest : ProductServiceIntegrationBase
13     {
14         /// <summary>
15         /// product service client.
16         /// </summary>
17         private ProductServiceClient serviceInstance;
18
19         /// <summary>
20         /// Initialization test instance.
21         /// </summary>
22         [TestInitialize]
23         public void InitTestInstance()
24         {
25             serviceInstance = ProductServiceClient.CreateClient(ServiceAddressForPrd/*for dev*/);
26
27             this.CheckCurrentTestCaseIsRun(this.serviceInstance);//check current test case .
28         }
29
30         /// <summary>
31         /// Product service get product by pid test.
32         /// </summary>
33         [TestMethod]
34         public void ProductService_GetProductByPid_Test()
35         {
36             var testResult = serviceInstance.GetProductByPid(0393844);
37
38             Assert.AreNotEqual(testResult, null);
39             Assert.AreEqual(testResult.Pid, 0393844);
40         }
41
42         /// <summary>
43         /// Product service delete search index test.
44         /// </summary>
45         [TestMethod]
46         public void ProductService_DeleteProductSearchIndex_Test()
47         {
48             var testResult = serviceInstance.DeleteProductSearchIndex();
49
50             Assert.IsTrue(testResult);
51         }
52     }
53 }