猿问

如何在“.NET-Core”中测试处理 HttpPatch 请求?

需求如下:



 有一个WebApi 是 HttpPatch 的源码如下:

[HttpPatch]
        [Route("test/{testId}")]
        public async Task<IActionResult> UpdateA([FromBody]TestItem test, int testId)
        {
              //.....
         }


测试代码如下:


 [Fact]
        public async Task UpdateATest()
        {
            var requestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), $"test/{testId}")
            {
                Content = new ObjectContent<dynamic>(new
                {
                    UserID = 1,
                    Title = "Second Queston From Web Api",
                    Content = "Second Question from Web Api"
                }, "application/json")
            };
            var response = await _httpClient.SendAsync(requestMessage);
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }

疑问



  在.NET-CoreObjectContent 是不支持的,请问,我需要如何进行测试 HttpPatch的请求呢?

杨魅力
浏览 475回答 1
1回答
随时随地看视频慕课网APP
我要回答