AttributeRouting不HttpConfiguration对象工作编写集成测试 [英] AttributeRouting not working with HttpConfiguration object for writing Integration tests
问题描述
我创建下面这里列出的一些想法集成测试:
<一href=\"http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/\">http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/
I'm creating some integration tests following the ideas outlined here: http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/
当我尝试注册从手工制作的HttpConfiguration对象,我发现了以下错误路线:
约束项inboundHttpMethod'与路径模板API /联系人/(编号)航线上必须有一个字符串值,或者是它实现IHttpRouteConstraint的类型。
When I try to register routes from a hand crafted HttpConfiguration object I'm getting the following error: "The constraint entry 'inboundHttpMethod' on the route with route template 'api/Contacts/{id}' must have a string value or be of a type which implements 'IHttpRouteConstraint'."
样code:
控制器:
Sample code: Controller:
[RoutePrefix("api")]
public class ContactsController : ApiController
{
[GET("Contacts/{id}",RouteName="GetContactsById")]
public ContactDTO Get(int id)
{
return new ContactDTO{ ID =1, Name="test"};
}
}
}
识别TestClass(MSTest的):
TestClass (MSTest):
[TestClass]
public class ContactsTest
{
private string _url = "http://myhost/api/";
private static HttpConfiguration config = null;
private static HttpServer server = null;
private HttpRequestMessage createRequest(string url, string mthv, HttpMethod method)
{
var request = new HttpRequestMessage();
request.RequestUri = new Uri(_url + url);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mthv));
request.Method = method;
return request;
}
private HttpRequestMessage createRequest<T>(string url, string mthv, HttpMethod method, T content, MediaTypeFormatter formatter) where T : class
{
HttpRequestMessage request = createRequest(url, mthv, method);
request.Content = new ObjectContent<T>(content, formatter);
return request;
}
[ClassInitializeAttribute]
public static void ClassInitialize(TestContext ctx)
{
config = new HttpConfiguration();
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
config.Services.Replace(
typeof(IDocumentationProvider), new DocProvider());
config.Services.Replace(
typeof(IApiExplorer),
new VersionedApiExplorer(config));
config.Services.Replace(
typeof(IHttpControllerSelector),
new VersionHeaderVersionedControllerSelector
(config)
);
AttributeRoutingHttpConfig.RegisterRoutes(config.Routes);
WebApiConfig.Register(config);
server = new HttpServer(config);
}
public static void ClassCleanup()
{
config.Dispose();
server.Dispose();
}
[TestMethod]
public void RetrieveContact()
{
var request = createRequest("Contacts/12","application/json",HttpMethod.Get);
var client = new HttpClient(server);
using (HttpResponseMessage response = client.SendAsync(request).Result)
{
Assert.IsNotNull(response.Content);
}
}
}
上线client.SendAsync出现错误。我检查config.Routes和数据类型的约束为'inboundHttpMethod'是AttributeRouting.Web.Http.WebHost.Constraints.InboundHttpMethodConstraint
看来,一个字符串值的预期。
任何帮助将是非常美联社preciated。
The error occurs on the line "client.SendAsync". I inspected config.Routes and the datatype for the "Constraints" for ''inboundHttpMethod' ' is AttributeRouting.Web.Http.WebHost.Constraints.InboundHttpMethodConstraint It appears that a string value is expected. Any help would be much appreciated.
推荐答案
有同样的问题。找到答案在这里:
Had the same problem. Found the answer here:
<一个href=\"https://github.com/mccalltd/AttributeRouting/issues/191\">https://github.com/mccalltd/AttributeRouting/issues/191
您需要更换
AttributeRoutingHttpConfig.RegisterRoutes(config.Routes);
与
config.Routes.MapHttpAttributeRoutes(cfg =>
{
cfg.InMemory = true;
cfg.AutoGenerateRouteNames = true;
cfg.AddRoutesFromAssemblyOf<ContactsController>();// Or some other reference...
});
我发现我也需要AutoGenerateRouteNames部分了。
I found I also needed the AutoGenerateRouteNames part, too.
这篇关于AttributeRouting不HttpConfiguration对象工作编写集成测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!