1

Web API 2 (5.0)로 업그레이드 한 후 제대로 작동하지 않는 통합 테스트가 거의없고 다음 코드를 호출 :HttpServer 내부에서 ApiController.Url을 사용할 때 WebAPI 경로를 찾을 수 없음

string uri = Url.Link(HttpRouteConstants.RouteName, new { param1 = 
paramValue1, param2 = paramValue2 }); 

는 기본적으로 Url.Link이 경로를 찾을 수 없습니다 (적어도 나는이 문제라고 생각합니다). 나는 그 문제는 우리가

GlobalConfiguration.Configure(WebApiConfig.Register); 

WebApiConfig.Register(config) 

에서 WebApiConfig 등록 호출

을 변경했다 HttpConfiguration & 경로가 웹 API (2)에 등록하는 방법을 생각

그리고 이제 통합 테스트 설치를 사용할 때 HttpServerHttpConfiguration 인스턴스와 같이 호출 할 수 없습니다. GlobalConfiguration.Configure (WebApiConfig.Register);하지만 이전 방법으로 다음을 수행하고 있습니다. WebApiConfig.Register (config)이 올바르게 작동하지 않는 것 같습니다. 참고로 내 라우팅 구성은 WebApiConfig에 있습니다.

해결 방법은 누구나 알고 있습니까?

  • VS 2013 (.NET 4.5.1)
  • WebAPI 2 (규칙 기반 라우팅 & 속성 라우팅)
  • xUnit의
  • Ninject에 :

    은 내가 사용 스택입니다

업데이트

여기

당신이 당신의 통합 테스트가 같은 모양을 공유 할 수

// Create configuration 
HttpConfiguration config = new HttpConfiguration(); 

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;    
config.DependencyResolver = new NinjectDependencyResolver(Kernel); 

//WebApiConfig.Register(config); Below is the excerpt from WebApiConfig 
config.EnableCors((System.Web.Http.Cors.ICorsPolicyProvider)config.DependencyResolver.GetService(typeof(System.Web.Http.Cors.ICorsPolicyProvider))); 

config.MapHttpAttributeRoutes(); 

config.Routes.MapHttpRoute(
    name: HttpRouteConstants.ApplicationAPIRouteName, 
    routeTemplate: CoreRouteConstants.DefaultApplicationControllerRoute + "/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 

config.Routes.MapHttpRoute(
    name: HttpRouteConstants.DefaultAPIRouteName, 
    routeTemplate: CoreRouteConstants.DefaultControllerRoute + "/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 


HttpServer server = null; 
try 
{ 
    server = new HttpServer(config); 
} 
catch (Exception ex) 
{ 
    throw ex; 
} 

string controller = String.Format("{0}{1}/api/MyController/{2}/", HttpServerBaseAddress, param1, param2); 
var client = new HttpClient(Server); 
using (HttpResponseMessage response = client.DeleteAsync(controller, new CancellationTokenSource().Token).Result) 
{ 
    response.IsSuccessStatusCode.Should().BeFalse(); 
    response.StatusCode.Should().Be(HttpStatusCode.NotFound); 
} 

//Below is the code from MyController 
public virtual async Task<HttpResponseMessage> DeleteAsync([FromUri]object key) 
{ 
    HttpResponseMessage response = null; 
    HttpStatusCode statusCode = HttpStatusCode.OK; 
    bool result = await Store.DeleteAsync(key); 
    if (!result) 
     statusCode = HttpStatusCode.NotFound; 
    response = Request.CreateResponse<bool>(statusCode, result); 
    string uri = Url.Link(HttpRouteConstants.ApplicationAPIRouteName, new { param1 = "param1", id = key }); 
    //NOTE: uri is null here as I mentioned 
    response.Headers.Location = new Uri(uri); 
    return response; 
} 

감사

답변

0

통합 테스트 코드 샘플입니까? 링크를 생성 할 수있는 통합 테스트 시나리오를 시도 했으므로 코드가 어떻게 생겼는지 궁금합니다. 내 시나리오에서는 Ninject를 사용하지 않았습니다.

GlobalConfiguration.Configure(WebApiConfig.Register); 이후 모든 웹 API 구성이 완료되면 HttpConfiguration.EnsureInitialized()을 호출해야합니다. 당신이 당신의 통합 테스트에 WebApiConfig.Register를 호출하는 경우

그래서, 당신은 웹 API 구성을 완료하고이 문제가 해결되는지 확인한다 HttpConfiguration.EnsureInitialized() 후 전화 시도 할 수 있습니다.

+0

'HttpConfiguration.ConfigureInitialized()'을 (를) 찾을 수 없습니다. 확장 메소드이고 어디에서 찾을 수 있는지 알려주세요. 감사합니다 – khorvat

+0

내 잘못 ...나는 HttpConfiguration의 EnsureInitialized 확장을 의미했다. –

+0

아, 미안해. 내가 알지 못했지만 ... _EnsureInitialized_를 시도했지만 여전히 오류가있어 _HttpServer_ & _Url.Link_에 문제가 있다고 가정한다. – khorvat

관련 문제