在内存集成测试中,web api失败了

时间:2013-11-10 04:46:15

标签: unit-testing asp.net-web-api integration-testing

这是我的Web APi内存集成测试。我得到"内部服务器错误"作为任何请求的响应。我无法确定问题。请让我知道这里有什么问题..... ??

public void mytest()
        {
            Type sampleTestsControllerType = typeof(WebAPI.Controllers.MyController);
            var config = new HttpConfiguration();
            config.Routes.MapHttpRoute("Default", "api/GetItem/{ItemId}/{modId}/{country}",
            new
            {
                controller = "My",
                ItemId= UrlParameter.Optional,
                modid= UrlParameter.Optional,
                country= UrlParameter.Optional,
                Action = "GetItem"
            });

            var server = new HttpServer(config);
            using (var client = new HttpMessageInvoker(server))
            {
                using (var request = 
                    new HttpRequestMessage(HttpMethod.Get, "http://localhost:26581/api/GetItem/201/10/IND")
                    )
                using (HttpResponseMessage response = client.SendAsync(request, CancellationToken.None).Result)
                {
                    Assert.Equals(HttpStatusCode.Found, response.StatusCode);
                }
            }
        }

提前致谢....

更新:这就是我的控制器的样子......

//Arguments null check
        if (IsArgumentNull(param1, param2, param3))
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, GlobalConstants.ArgumentsRequired);
        }

        IContentNegotiator contentNegotiator = Configuration.Services.GetContentNegotiator();


        ContentNegotiationResult contentNegotiationResult = contentNegotiator.Negotiate(typeof(returnObject), Request, Configuration.Formatters);


        if (contentNegotiationResult == null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, GlobalConstants.NotAcceptable);
        }


        var result= _repository.GetItem(param1, param2, param3);


        if (result== null)
        {
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, GlobalConstants.NotFound);
        }

        return new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ObjectContent<returnObject>(result, contentNegotiationResult.Formatter, contentNegotiationResult.MediaType.MediaType)
        };

0 个答案:

没有答案