我为异常编写了一个单元测试。但看起来它无法正常工作。它总是说“404 Not Found”状态。这意味着找不到 url 请求。如果我在浏览器上粘贴相同的网址,它会HttpResponse.StatusCode
显示BAD REQUEST
。
我不明白为什么它不适用于单元测试。
[TestMethod()]
public void GetTechDisciplinesTestException()
{
var config = new HttpSelfHostConfiguration("http://localhost:51546/");
config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
using (var server = new HttpSelfHostServer(config))
using (var client = new HttpClient())
{
server.OpenAsync().Wait();
using (var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:51546/api/techdisciplines/''"))
using (var response = client.SendAsync(request).Result)
{
//Here Response Status Code says 'Not Found',
//Suppose to be 'Bad Request`
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
}
server.CloseAsync().Wait();
};
}
我尝试使用HttpSelfHostServer
哪个工作正常,它使用 IISExpress。
[TestMethod()]
public void GetTechDisciplinesTestException()
{
using (var client = new HttpClient())
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:51546/api/techdisciplines/''"))
using (var response = client.SendAsync(request).Result)
{
Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode);
}
};
}
所以我不知道HttpSelfHostServer
代码中没有 wkring 吗?如何强制HttpSelfHostServer
使用IISExpress
?这个怎么处理?