2
我想了解如何运行基于自我主机配置的集成测试。在内存中HTTP服务器Asp.net WebAPI
在下面的代码中,我应该注册我的配置与WebApiConfig。注册或不注册似乎没有区别。
完整的管道是真的被测试还是这是一个错觉?因为,如果我没有使用配置和我的API中定义的路由,而是像我这里所做的那样声明自己的路线,那么我可能只是没有测试完整的pipleine。
有没有其他方法可以完全测试api。下面的代码正在测试除了我的管道之外的很多东西(比如客户端,SelfHosting等)。这对我来说似乎是一种矫枉过正。有任何想法吗 ?
var config = new HttpSelfHostConfiguration("http://localhost:9090/");
config.Routes.MapHttpRoute("Default", "{api}/{controller}/{id}", new { id = RouteParameter.Optional });
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
MyApiProject.WebApiConfig.Register(config);
using (var server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
using (var client = new HttpClient())
{
using (var response = client.PostAsync("http://localhost:9090/api/login",
new FormUrlEncodedContent(new List<KeyValuePair<string,string>> { new KeyValuePair<string, strin("Foo","Bar)}), CancellationToken.None).Result)
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
using (var response = client.GetAsync("http://localhost:9090/api/login").Result)
{
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
server.CloseAsync().Wait();
}
我不是在寻找单元测试,它们是分开的,有针对性,正如你所说的。在我在问题中提到的测试中使用我在api中指定的配置的正确方法是什么? – 2013-03-08 08:01:43
你可以调用相同的代码,即添加所有路线和其他东西。我相信'MyApiProject.WebApiConfig.Register(config);'只是这样做的。 – 2013-03-08 22:18:38