如何在ASP.NET Core项目中使用MStest测试Ok()结果

问题描述:

我正在使用MStest来测试我的控制器.

I'm using MStest for testing my controllers.

我要测试此操作:

[HttpGet(Name = "GetGroups")]
public async Task<IActionResult> Get()
{
    var groups = await _unitOfWork.Repository<Groupe>().GetAllAsync();
    var groupsDto = Mapper.Map<IEnumerable<GroupDto>>(groups);
    if (groupsDto.Count() == 0)
    {
        return NotFound();
    }
    return Ok(groupsDto);
}

此操作的测试之一如下所示:

One of the test for this action looks like that:

[TestMethod]
public async Task Group_Get_Should_Return_InstanceOfTypeOkNegotiatedContentResultIEnumerableGroupDto()
{
    // Arrange
    moqGroupRepository.Setup(g => g.GetAllAsync(null)).ReturnsAsync(groups).Verifiable();
    moqUnitOfWork.Setup(x => x.Repository<Groupe>()).Returns(moqGroupRepository.Object);

    var controller = new GroupController(moqUnitOfWork.Object);

    // Act
    var actionResult = await controller.Get() as OkNegotiatedContentResult<IEnumerable<GroupDto>>;

    // Assert
    Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult<IEnumerable<GroupDto>>));
}

这里的问题是 OkNegotiatedContentResult 在ASP.Net Core项目测试中未知.

The problem here is OkNegotiatedContentResult is unknown in ASP.Net Core project test.

我应该使用什么来测试Ok()结果?

What should I use to test Ok() result?

这里的问题是OkNegotiatedContentResult在ASP网络中是未知的 核心项目测试我应该使用什么来测试Ok()结果?

The probleme here is OkNegotiatedContentResult is unknow in asp net core project test What should i use to test Ok() result?

您可以通过安装定义了IActionResult实现的Microsoft.AspNetCore.Mvc NuGet软件包来解决此问题.

You could fix the problem by installing Microsoft.AspNetCore.Mvc NuGet package where implementations of IActionResult are defined.

但是,ASP.NET Core不包含OkNegotiatedContentResult类型,它来自ASP.NET Web API.在ASP.NET Core中,Controller.Ok()方法返回OkObjectResult类型的实例.

However ASP.NET Core does not contain OkNegotiatedContentResult type, it's from ASP.NET Web API. In ASP.NET Core Controller.Ok() method returns the instance of OkObjectResult type.

这两个语句中的检查也不一致:

You also have inconsistent checks in these two statements:

var actionResult = await controller.Get() as OkNegotiatedContentResult<IEnumerable<GroupDto>>;
Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult<IEnumerable<GroupDto>>));

如果无法将对象强制转换为请求的类型,则

as运算符将返回null.因此,您可以将第二张支票替换为以下内容:

as operator will return null if object could not be cast to requested type. So you could replace the second check with the following:

Assert.IsNotNull(actionResult);

所以必需的步骤是:

  1. Microsoft.AspNetCore.Mvc NuGet软件包安装到您的测试项目中.
  2. 通过以下方式调整测试代码:

  1. Install Microsoft.AspNetCore.Mvc NuGet package to your Test project.
  2. Adjust the test code in the following way:

// ...
using Microsoft.AspNetCore.Mvc;

[TestMethod]
public async Task Group_Get_Should_Return_InstanceOfTypeOkNegotiatedContentResultIEnumerableGroupDto()
{
    // Arrange
    moqGroupRepository.Setup(g => g.GetAllAsync(null)).ReturnsAsync(groups).Verifiable();
    moqUnitOfWork.Setup(x => x.Repository<Groupe>()).Returns(moqGroupRepository.Object);

    var controller = new GroupController(moqUnitOfWork.Object);

    // Act
    var actionResult = await controller.Get() as OkObjectResult;

    // Assert
    Assert.IsNotNull(actionResult);
    Assert.IsInstanceOfType(actionResult.Value, typeof(IEnumerable<GroupDto>));
}