[英]Unit Testing / Integration Testing Web API with HttpClient in Visual Studio 2013
我很難用Visual Studio 2013測試我的API控制器。我的一個解決方案有一個Web API項目和一個測試項目。 在我的測試項目中,我有一個單元測試:
[TestMethod]
public void GetProduct()
{
HttpConfiguration config = new HttpConfiguration();
HttpServer _server = new HttpServer(config);
var client = new HttpClient(_server);
var request = new HttpRequestMessage
{
RequestUri = new Uri("http://localhost:50892/api/product/hello"),
Method = HttpMethod.Get
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var response = client.SendAsync(request).Result)
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
var test = response.Content.ReadAsAsync<CollectionListDTO>().Result;
}
}
我一直得到404 。 我嘗試使用一個Visual Studio實例(IIS Express)運行我的API,並嘗試在另一個實例中調試此單元測試。 但沒有運氣。 我已經驗證我可以將此URL放在瀏覽器中(當一個Visual Studio正在調試時),我看到了我的JSON響應。 但我無法弄清楚如何使用我的單元測試和HttpClient
。 我試圖在網上找到例子,但似乎找不到一個。 有人可以幫忙嗎?
更新1:我嘗試添加路線但沒有發生任何事情。
HttpConfiguration config = new HttpConfiguration();
// Added this line
config.Routes.MapHttpRoute(name: "Default", routeTemplate: "api/product/hello/");
HttpServer _server = new HttpServer(config);
var client = new HttpClient(_server);
[...rest of code is the same]
這是我的API控制器
[HttpGet]
[Route("api/product/hello/")]
public IHttpActionResult Hello()
{
return Ok();
}
更新解決方案:如果我在沒有HttpServer
對象的情況下新建HttpClient
我能夠使它工作。 我仍然需要運行兩個VS實例。 1運行我的API代碼,另一個運行單元測試。
這是一種工作方法。
[TestMethod]
public void Works()
{
var client = new HttpClient(); // no HttpServer
var request = new HttpRequestMessage
{
RequestUri = new Uri("http://localhost:50892/api/product/hello"),
Method = HttpMethod.Get
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var response = client.SendAsync(request).Result)
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
任何人都知道為什么它不能與HttpServer
和傳遞給HttpClient
的HttpConfiguration
HttpClient
? 我見過很多使用它的例子。
參考以下文章,我能夠做到......
通過使用傳遞給HttpClient
的HttpServer
和HttpConfiguration
。 在下面的示例中,我創建了一個使用屬性路由的簡單ApiController
。 我將HttpConfiguration
配置為映射屬性路由,然后將其傳遞給新的HttpServer
。 然后, HttpClient
可以使用配置的服務器對測試服務器進行集成測試調用。
public partial class MiscUnitTests {
[TestClass]
public class HttpClientIntegrationTests : MiscUnitTests {
[TestMethod]
public async Task HttpClient_Should_Get_OKStatus_From_Products_Using_InMemory_Hosting() {
var config = new HttpConfiguration();
//configure web api
config.MapHttpAttributeRoutes();
using (var server = new HttpServer(config)) {
var client = new HttpClient(server);
string url = "http://localhost/api/product/hello/";
var request = new HttpRequestMessage {
RequestUri = new Uri(url),
Method = HttpMethod.Get
};
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var response = await client.SendAsync(request)) {
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
}
}
public class ProductController : ApiController {
[HttpGet]
[Route("api/product/hello/")]
public IHttpActionResult Hello() {
return Ok();
}
}
}
為了集成測試控制器,不需要運行另一個VS實例。
以下簡化版本的測試也可以使用
var config = new HttpConfiguration();
//configure web api
config.MapHttpAttributeRoutes();
using (var server = new HttpServer(config)) {
var client = new HttpClient(server);
string url = "http://localhost/api/product/hello/";
using (var response = await client.GetAsync(url)) {
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
在您的情況下,您需要確保正確配置服務器以匹配您的Web api設置。 這意味着您必須使用HttpConfiguration
對象注冊api路由。
var config = new HttpConfiguration();
//configure web api
WebApiConfig.Register(config);
//...other code removed for brevity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.