Test ASP.API controller with xUnit: can't assert Type
Hello 
I hope my question is not too dumb but I m stuck on the testing of my API controller. (It is my first test project)
Me controller looks exactly like this :
And i am trying to test the first case : Recipes.Any() = false, in this test :
But it doesnt work. Both my IsType<NotFoundResult>(result) and Equal(controller.NotFound(), result) fail. How could i test this please?
I hope my question is not too dumb but I m stuck on the testing of my API controller. (It is my first test project)
Me controller looks exactly like this :
[HttpGet]
public async Task<ActionResult<IEnumerable<RecipeDto>>> GetRecipes(
[FromQuery] int pageNumber,
[FromQuery] int pageSize,
[FromQuery] string? title,
[FromQuery] string? searchQuery)
{
try
{
(IEnumerable<Recipe> recipes, PaginationMetadata metadata) = await _recipeService.GetPage(pageNumber, pageSize, title, searchQuery);
if (recipes is null || recipes.Any() == false)
{
_logger.LogInformationGetAll(nameof(Recipe));
return NotFound();
}
IEnumerable<RecipeDto> response = recipes.Select(r => r.MapToRecipeDto());
Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(metadata));
return Ok(response);
}
catch (Exception ex)
{
_logger.LogCriticalGetAll(nameof(Recipe), ex);
return this.InternalErrorCustom();
}
}[HttpGet]
public async Task<ActionResult<IEnumerable<RecipeDto>>> GetRecipes(
[FromQuery] int pageNumber,
[FromQuery] int pageSize,
[FromQuery] string? title,
[FromQuery] string? searchQuery)
{
try
{
(IEnumerable<Recipe> recipes, PaginationMetadata metadata) = await _recipeService.GetPage(pageNumber, pageSize, title, searchQuery);
if (recipes is null || recipes.Any() == false)
{
_logger.LogInformationGetAll(nameof(Recipe));
return NotFound();
}
IEnumerable<RecipeDto> response = recipes.Select(r => r.MapToRecipeDto());
Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(metadata));
return Ok(response);
}
catch (Exception ex)
{
_logger.LogCriticalGetAll(nameof(Recipe), ex);
return this.InternalErrorCustom();
}
}And i am trying to test the first case : Recipes.Any() = false, in this test :
[Fact]
public async Task GetRecipes_Empty_ReturnsNotFound()
{
// Arrange
Tools.RecipeService recipeService = new Tools.RecipeService();
RecipesController controller = new RecipesController(new NullLogger<RecipesController>(), recipeService);
// Act
ActionResult<IEnumerable<RecipeDto>> result = await controller.GetRecipes(1, 5, null, null);
// Assert
Assert.NotNull(result);
Assert.IsType<ActionResult<IEnumerable<RecipeDto>>>(result);
Assert.IsType<NotFoundResult>(result);
Assert.Equal(controller.NotFound(), result);
} [Fact]
public async Task GetRecipes_Empty_ReturnsNotFound()
{
// Arrange
Tools.RecipeService recipeService = new Tools.RecipeService();
RecipesController controller = new RecipesController(new NullLogger<RecipesController>(), recipeService);
// Act
ActionResult<IEnumerable<RecipeDto>> result = await controller.GetRecipes(1, 5, null, null);
// Assert
Assert.NotNull(result);
Assert.IsType<ActionResult<IEnumerable<RecipeDto>>>(result);
Assert.IsType<NotFoundResult>(result);
Assert.Equal(controller.NotFound(), result);
}But it doesnt work. Both my IsType<NotFoundResult>(result) and Equal(controller.NotFound(), result) fail. How could i test this please?