1

所以我正在尝试使用内存中的 Http 服务器测试我的 API,正如这篇博文所建议的那样:http: //blogs.msdn.com/b/youssefm/archive/2013/01/28/writing-tests-for -an-asp-net-webapi-service.aspx

我的代码如下所示:

[TestMethod]
    public void AddNewCustomer()
    {
        config = new HttpConfiguration();
        WebApiConfig.Register(config);
        HttpServer server = new HttpServer(config);
        using (HttpMessageInvoker client = new HttpMessageInvoker(server))
        {
            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Customer"))

            {
                request.Content = new StringContent(@"{ ""Email"" : ""me@you.com"", ""Password"" : ""password"" }");
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                using (HttpResponseMessage response = client.SendAsync(request, CancellationToken.None).Result)
                {
                    Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
                }
            }
        };
    }

我已经尝试测试实际的 API,当我发布客户时它会正确响应。当我尝试用它进行测试时,response.StatusCode 是“内部服务器错误”。

这很烦人,但更令人困惑的是,我发现自己无法调试实际出了什么问题——我不知道为什么在这种环境下会出现这个错误,而且我无法插入断点来测试正在发生的事情。

编辑:我的回复正文只是:

        "{\"Message\":\"An error has occurred.\"}"

有任何想法吗?

4

1 回答 1

0

只需在 new HttpConfiguration() 之后添加以下内容

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
于 2014-05-02T19:22:19.803 回答