简体   繁体   English

使用ASP.NET Core在单元测试中模拟POST请求

[英]Simulate POST request in a unit test using ASP.NET Core

I am currently implementing unit tests in a ASP.NET Core project and I have to test the POST method of an API Controller. 我目前正在ASP.NET Core项目中实现单元测试,我必须测试API控制器的POST方法。 Here is an example of the POST method: 以下是POST方法的示例:

[HttpPost]
public IActionResult Post([FromBody]Product product)
{
    if (!ModelState.IsValid)
    {
        return BadRequest();
    }

    try
    {
        var returnValue = productService.Save(product);
        return CreatedAtRoute(nameof(Post), new { returnValue = returnValue }, product);
    }
    catch
    {
        return BadRequest();
    }

}

And here is an example of the model I am using: 这是我正在使用的模型的一个例子:

public class Product
{
    [Required]
    [MaxLength(25)]
    public string Name { get; set; }

    [MaxLength(200)]
    public string Description { get; set; }
}

The main idea is to test both Created (201) and also Bad Request (400) results. 主要思想是测试Created(201)和Bad Request(400)结果。 I went through this page and the Created (201) works pretty fine. 我浏览了这个页面 ,Created(201)工作得非常好。 However, when I applied the same logic for the Bad Request (401) it didn't work since I am not making a real request. 但是,当我为Bad Request(401)应用相同的逻辑时,它没有工作,因为我没有提出真正的请求。 But when I tried using PostMan with the "wrong" values I got 400, as expected. 但是当我尝试使用“错误”值的PostMan时,我得到了400,正如预期的那样。

How can I simulate a POST request from a unit test? 如何从单元测试中模拟POST请求? Or am I missing something? 或者我错过了什么?

The documentation you went through is for classic ASP.NET. 您经历的文档适用于经典ASP.NET。 Look at ASP.NET Core docs instead: Integration testing . 请查看ASP.NET核心文档: 集成测试

There is the TestServer class designed for controller testing in ASP.NET Core: 有一个TestServer类专为ASP.NET Core中的控制器测试而设计:

_server = new TestServer(new WebHostBuilder()
    .UseStartup<Startup>());
_client = _server.CreateClient();

var content = new StringContent($"username={_username}&password={_password}",
    Encoding.UTF8,
    "application/x-www-form-urlencoded");

HttpResponseMessage response = await _client.PostAsync("foo_path", content);

Remarks: 备注:

  • TestServer is parametrized by Startup class. TestServerStartup类进行参数化。 Probably you would create a separate Startup class for testing or override its methods in some way to mock dependencies. 可能你会创建一个单独的Startup类来测试或以某种方式覆盖它的方法来模拟依赖。

  • An in-memory server instance is accessible only from a client created by _server.CreateClient() call. 只能从_server.CreateClient()调用创建的客户端访问内存服务器实例。 The client is created with a special HttpMessageHandler inside. 使用特殊的HttpMessageHandler创建客户端。 That handler allows to directly call APIs under test without exposing the in-memory instance as a real HTTP server. 该处理程序允许直接调用正在测试的API,而不会将内存中实例暴露为真正的HTTP服务器。

Another option might be used for integration testing is to run a "real" Kestrel server to test your web API. 可能用于集成测试的另一个选项是运行“真正的”Kestrel服务器来测试您的Web API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM