1

したがって、このブログ投稿でアドバイスされているように、メモリ内の HTTP サーバーを使用して API をテストしようとしています。 -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 に答える