use*_*117 8 c# unit-testing asp.net-web-api asp.net-web-api-routing
我有一个带路由属性的控制器.此控制器在单元测试中失败,因为找不到路由:
在路径集合中找不到名为"Values"的路径
这是控制器方法:
[Route("api/values", Name="ApiValues")]
[HttpGet]
public HttpResponseMessage Get()
{
urlHelper.Link("ApiValues", new {});
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试:
var valuesController = new ValuesController()
{
Request = new HttpRequestMessage
{
RequestUri = new Uri("http://localhost/api/")
},
Configuration = new HttpConfiguration()
};
valuesController.Get();
Run Code Online (Sandbox Code Playgroud)
我还尝试将其添加到单元测试中:
valuesController.Configuration.MapHttpAttributeRoutes();
valuesController.Configuration.EnsureInitialized();
Run Code Online (Sandbox Code Playgroud)
但这没有任何帮助.
Fen*_*hao 13
我得到了同样的错误:
A route named 'Values' could not be found in the route collection.
Run Code Online (Sandbox Code Playgroud)
但是,单元测试通过我的机器上后,我加MapHttpAttributeRoutes和EnsureInitialized:
var valuesController = new ValuesController()
{
Request = new HttpRequestMessage { RequestUri = new Uri("http://localhost/api/") },
Configuration = new HttpConfiguration()
};
valuesController.Configuration.MapHttpAttributeRoutes();
valuesController.Configuration.EnsureInitialized();
valuesController.Get();
Run Code Online (Sandbox Code Playgroud)
您能否提供更多信息来重现问题或检查我们的测试代码之间是否有任何区别?