简体   繁体   English

如何在nunit中正确获取控制器的结果?

[英]How to properly get the result of controller in nunit?

I have a ASP.NET Core WebAPI project and I´m having some trouble to test the controllers. 我有一个ASP.NET Core WebAPI项目,我在测试控制器时遇到了一些麻烦。

In my controller I have the following action: 在我的控制器中,我有以下操作:

[HttpGet]
public async Task<ActionResult<ListUsersDto>> Get()
{
    var users = await _userService.GetAllUsers();
    return Ok(users);
}

So my test is: 所以我的测试是:

[Test]
public async Task Get_ReturnsAllUsers()
{
    var listUsersDto = new ListUsersDto();
    listUsersDto.Users.Add(
           new UserDto()
           {
               Id = DEFAULT_TRUCK_ID,
               Color = "White",
               CreationDate = date,
               EditDate = date,
               ManufactureYear = date.Year,
               ModelYear = date.Year
            }
    );
    _userService.Setup(m => m.GetAll()).ReturnsAsync(listUsersDto);
    var controller = GetController();
    var response = await controller.Get();            
    Assert.IsInstanceOf(typeof(OkObjectResult), response.Result);
}

This test passes normally. 该测试正常通过。 But I cant figure how to get the actual values of the return. 但我不知道如何获得回报的实际值。

Assert.AreEqual(1, response.Value.Users.Count);
//This test fails because the Value is empty.

The Value property of the response is always empty. responseValue属性始终为空。 If I debug the test I can see that the await controller.Get(); 如果我调试测试,我可以看到await controller.Get(); returns the correct object. 返回正确的对象。 What am I missing here? 我在这里错过了什么?

Because the action is defined to return ActionResult<ListUsersDto> and you are returning Ok(users) , then what you describe is by design. 因为操作被定义为返回ActionResult<ListUsersDto>并且您返回Ok(users) ,所以您描述的是设计。

Value will be empty because an action result is being returned. Value将为空,因为正在返回操作结果。 ActionResult<T> return one or the other. ActionResult<T>返回一个或另一个。 Not Both. 不是都。

Because of the Ok(users) you would need to extract the value from the returned action result. 由于Ok(users)您需要从返回的操作结果中提取值。

//Act
var response = await controller.Get();            

//Assert
OkObjectResult result = Assert.IsInstanceOf<OkObjectResult>(response.Result);
//assert the value within the object result.
ListUsersDto dto = Assert.IsInstanceOf<ListUsersDto>(result.Value);
Assert.AreEqual(1, dto.Users.Count);

Things would have been different is for example the action was defined differently 事情会有所不同,例如行动的定义不同

[HttpGet]
public async Task<ActionResult<ListUsersDto>> Get() {
    var users = await _userService.GetAllUsers();

    if(users == null || users.Users.Count == 0)
        return NoContent(); //Or some other relevant action result

    return users;
}

And the test tested like you did originally 测试测试就像你原来做的那样

[Test]
public async Task Get_ReturnsAllUsers() {
    //Arrange
    var listUsersDto = new ListUsersDto();
    listUsersDto.Users.Add(
           new UserDto()
           {
               Id = DEFAULT_TRUCK_ID,
               Color = "White",
               CreationDate = date,
               EditDate = date,
               ManufactureYear = date.Year,
               ModelYear = date.Year
            }
    );
    _userService.Setup(_ => _.GetAllUsers()).ReturnsAsync(listUsersDto);
    var controller = GetController();

    //Act
    var response = await controller.Get();

    //Assert
    Assert.AreEqual(1, response.Value.Users.Count);
}

Then because the action returned the result object directly, then the ActionResult<ListUsersDto>.Value property would be populated. 然后,因为操作直接返回结果对象,所以将填充ActionResult<ListUsersDto>.Value属性。

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

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