我很难用Visual Studio 2013测试我的API控制器。我的一个解决方案有一个Web API项目和一个Test项目。在我的测试项目中,我对此进行了单元测试:
[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();
}
更新解决方案:如果我HttpClient
没有HttpServer
对象就可以进行更新。我仍然需要运行两个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
和HttpConfiguration
传递给一起使用HttpClient
吗?我已经看到许多使用此示例。
参考以下文章,我能够做到...
通过与HttpServer
和一起HttpConfiguration
进入HttpClient
。在下面的示例中,我创建了一个ApiController
使用属性路由的简单方法。我配置了HttpConfiguration
以映射属性路由,然后将其传递给new 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句