public void Test_不是聖誕節的時候_應回傳BadRequest() { // arrange // Step1: 初始化 fakeInnerHandler, 拿來接在欲測試的 messageHandler 後面 // 預計的回傳值可以隨便自訂 var fakeInnerHandler = new FakeInnerHandler { Message = new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent("Fake inner handler response") } }; // Step2: 初始化測試目標,並將 fakeInnerHandler assign 給 InnerHandler 的 property var target = new XmasMessageHandler() { InnerHandler = fakeInnerHandler }; // Step3: MessageHandler 的單元測試,需要透過HttpMessageInvoker來模擬 client 端送 request 進來 HttpMessageInvoker client = new HttpMessageInvoker(target); var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/Fake"); httpRequestMessage.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); // act // Step4: 呼叫HttpMessageInvoker的 SendAsync ,就會先呼叫 target, // 當正常執行時,會先呼叫 target 的 SendAsync(), 穿過 target 後,再呼叫 InnerHandler 的 SendAsync() var actual = client.SendAsync(httpRequestMessage, new CancellationToken()).Result; // assert // Step5: 驗證回傳的 StatusCode 是否為 BadRequest Assert.AreEqual(HttpStatusCode.BadRequest, actual.StatusCode); }
public void Test_聖誕節的時候_才可以正常呼叫() { // Question: 怎麼模擬今天是聖誕節? // 將 Datetime.Now 拉出來讓外部可以注入 // arrange // Step1: 初始化 fakeInnerHandler, 拿來接在欲測試的 messageHandler 後面 // 預計的回傳值可以隨便自訂 var fakeInnerHandler = new FakeInnerHandler { Message = new HttpResponseMessage(System.Net.HttpStatusCode.OK) { Content = new StringContent("Fake inner handler response") } }; // Step2: 初始化測試目標,並將 fakeInnerHandler assign 給 InnerHandler 的 property var target = new XmasMessageHandler() { InnerHandler = fakeInnerHandler }; target.MockNow = new DateTime(2014, 12, 25); // Step3: MessageHandler 的單元測試,需要透過HttpMessageInvoker來模擬 client 端送 request 進來 HttpMessageInvoker client = new HttpMessageInvoker(target); var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/Fake"); httpRequestMessage.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); // act // Step4: 呼叫HttpMessageInvoker的 SendAsync ,就會先呼叫 target, var actual = client.SendAsync(httpRequestMessage, new CancellationToken()).Result; // assert // Step5: 驗證回傳的 StatusCode 是否為 OK Assert.AreEqual(HttpStatusCode.OK, actual.StatusCode); // Step6: 驗證回傳的訊息內容,是否為 fakeInnerHandler 的 Message 內容 Assert.IsNotNull(actual.Content); var responseContent = actual.Content.ReadAsStringAsync().Result; Assert.AreEqual("Fake inner handler response", responseContent); }