How to get the Values from a Task returned through an API for Unit Testing

You can get tested controller without changing returned type.
IActionResult is base type for all others.
Cast result into expected type and compare returned value with expected.

Since you are testing asynchronous method, make test method asynchronous as well.

public async Task ConfigurationSearchGetTest()
    using (var context = GetContextWithData())
        var controller = new ConfigurationSearchController(context);
        var items = context.Configurations.Count();

        var actionResult = await controller.GetConfiguration(12);

        var okResult = actionResult as OkObjectResult;
        var actualConfiguration = okResult.Value as Configuration;

        // Now you can compare with expected values

Leave a Comment