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?
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.
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.
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.
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:
Microsoft.AspNetCore.Mvc
NuGet package to your Test project.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>));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With