Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Ok() result using MStest in ASP.NET Core project

I'm using MStest for testing my controllers.

I want to test this action:

[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>>));
}

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

What should I use to test Ok() result?

like image 931
Omar AMEZOUG Avatar asked Mar 27 '18 08:03

Omar AMEZOUG


People also ask

How do I run a test case in .NET core?

To run test in Visual Studio, let us open Test Explorer from the Test → Window → Test Explorer menu option. And you can see that Visual Studio automatically detects the test. The name of the test consists of namespace.

How do you add MSTest to a project?

On the File menu, select New > Project, or press Ctrl+Shift+N. On the Create a new project page, type unit test into the search box. Select the project template for the test framework that you want to use, for example MSTest Test Project or NUnit Test Project, and then select Next.

What is MSTest used for?

MSTest is one type of framework that provides the facility to test the code without using any third-party tool. It helps in writing effective unit tests using MSTest framework to test software applications. MSTest is a number-one open-source test framework that is shipped along with the Visual Studio IDE.


1 Answers

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

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

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 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);

So the required steps are:

  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>));
    }
    
like image 84
CodeFuller Avatar answered Oct 12 '22 11:10

CodeFuller