我认为在网络上进行的简单搜索远远不止于此。
最接近解决方案的是最先使用Attributes进行路由的解决方案:AttributeRouting无法与HttpConfiguration对象配合使用来编写集成测试
但是ASP.NET Web Api 2呢?
HttpConfiguration config = new HttpConfiguration();
// config.MapHttpAttributeRoutes(); // This doesn't work. I guess there is needed some more plumbing to know what Controllers to search for attributes, but I'm lost here.
HttpServer server = new HttpServer(config);
using (HttpMessageInvoker client = new HttpMessageInvoker(server))
{
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, "http://localhost/api/accounts/10"))
using(HttpResponseMessage response = client.SendAsync(request, CancellationToken.None).Result)
{
response.StatusCode.Should().Be(HttpStatusCode.Created);
}
}
如何注入控制器,以便它读取控制器上的属性并设置路由,以便实际上可以进行一些测试?
这太荒谬了...我使用它来工作:
config.MapHttpAttributeRoutes();
config.EnsureInitialized();
因此,基本上,这将运行的配置初始化config.MapHttpAttributeRoutes()
。我想,我会以为这是自动完成的。
但是现在可以了,我很高兴。
有关此问题的更多信息,请参见:http : //ifyoudo.net/post/2014/01/28/How-to-unit-test-ASPNET-Web-API-2-Route-Attributes.aspx
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句