Recently, I started working on an ASP.NET Core project, and while writing unit tests for the API endpoints, I encountered an error regarding converting 'Microsoft.AspNetCore.Mvc.ActionResult' to 'System.Collections.Generic.IEnumerable'. Fortunately, I managed to resolve this issue successfully. Now, let me share the solution with you so that you can also fix it. ProductUnitTest.cs
[TestFixture]
public class ClientUnitTests
{
[Test]
public async Task CheckCategory()
{
// Arrange
List<IEnumerable<string>> categoryList = new List<IEnumerable<string>>();
var links = await Controller.GetCategory();
categoryList.Add(links.Value);
foreach (var categoryLink in categoryList)
{
categoryList.Add(categoryLink);
}
// Assert
Assert.IsNotNull(categoryList);
Assert.GreaterOrEqual(0, categoryList.Count);
Assert.IsInstanceOf<IEnumerable<string>>(categoryList);
}
}
Controller.cs
[HttpGet]
public async Task<ActionResult<IEnumerable<string>>> GetCategory()
{
var result = await _client.GetCategory();
return Ok(result);
}
Category.cs
public async Task<ActionResult<IEnumerable<string>>> GetCategory()
{
}
But this code giving error Cannot convert from 'Microsoft.AspNetCore.Mvc.ActionResult' to 'System.Collections.Generic.IEnumerable<string>' and thisĀ error indicates that we're trying to add an ActionResult to a list of IEnumerable<string>, which is not allowed.To fix this, we need to ensure that we're adding the actual result value from the ActionResult to the list, rather than adding the entire ActionResult itself.
[TestFixture]
public class ClientUnitTests
{
[Test]
public async Task CheckCategory()
{
// Arrange
List<string> categoryList = new List<string>();
var actionResult = await Controller.GetCategory();
var links = actionResult.Value; // Extracting the value from the ActionResult
foreach (var link in links)
{
categoryList.Add(link);
}
// Assert
Assert.IsNotNull(categoryList);
Assert.GreaterOrEqual(0, categoryList.Count);
Assert.IsInstanceOf<IEnumerable<string>>(categoryList);
}
}
[HttpGet]
public async Task<ActionResult<IEnumerable<string>>> GetCategory()
{
var result = await _client.GetCategory();
return Ok(result);
}
public async Task<IEnumerable<string>> GetCategory()
{
// Some logic here
}
Assuming we're working with .NET Core , we have two options for the return type of our controller method GetCategory
:
The first option is to use
Task<ActionResult<IEnumerable<string>>>
, which explicitly indicates that our method returns an action result containing a collection of strings. This is the preferred approach as it provides clarity about the type of response expected.
The second option, although less preferred, is to use
Task<IActionResult>
This code simply indicates that our method returns any kind of action result and we prefer the first option as it offers better clarity and type safety, making our code easier to understand and maintain.Read Similar Articles
- What smart contract function is not used in honeypot tokens?
- Difference between View and Indexed View or Materialized
- How To Checking For Empty or Null JToken In a JObject in C#
- Solved Error : command validateembeddedbinary failed with a nonzero exit code
- How to execute SQL query in Entity Framework Core using C#
- Download Sample Mp4 Video | Download Mp4 Test Videos for Testing Purpose
- What is Sql Joins? With Realtime Examples
- How To Edit/Delete Selected Row From HTML Table Using JavaScript
- Fixed- "error: you need to specify a command before moving on. use '--help' to view the available commands."