0
public class crumbController: Controller

{

   public ActionResult<IEnumerable<Crumb>> GetBreadcrumb([FromRoute]int tag_X_ParentID)

   {

       try
       {
           Services.Interface.IBreadcrumbService service = new Services.Imp.BreadcrumbService(_configuration, _logger);

           if (service != null)
           {
               Breadcrumb breadcrumb = service.GetBreadcrumb(ParentID);
               return Ok(breadcrumb.crumbs.ToList());
            }
            else
            {
               return BadRequest("Unable to establish connection.");
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex.Message, ex);//log error
            return BadRequest(ex.Message);
        }

      }
 }

 public class crumbControllerShould
 {
    [Fact]
    public void GetBreadCrumbsTest()
    {
        IConfiguration _configuration;
        IHostingEnvironment _env;
        ILoggerManager _logger;

        int tag_X_ParentID = 12;


        var mock_logger = new Mock<ILoggerManager>();
        _logger = mock_logger.Object;
        var mock_configuration = new Mock<IConfiguration>();
        _configuration = mock_configuration.Object;

        var mock_env = new Mock<IHostingEnvironment>();

        mock_env
            .Setup(m => m.EnvironmentName)
            .Returns("http://localhost:8980/");

        _env = mock_env.Object;

        Assert.IsAssignableFrom<IHostingEnvironment>(_env);
        Assert.IsAssignableFrom<IConfiguration>(_configuration);
        Assert.IsAssignableFrom<ILoggerManager>(_logger);

        var controller = new BreadcrumbController(_configuration, _env, _logger);
        var result = controller.GetBreadcrumb(tag_X_ParentID);
        Assert.NotNull(result);
        Assert.IsType<IEnumerable<Crumb>>(result);                     
       //this line fails indicating that expected: System.Collections.Generic.IEnumerable'1[[FHH.crumpApi.Entitities.crumb, FHH.BreadcrumbApi]] 

      //Actual: Microsoft.AspNetCore.Mvc.ActionResult'1 [[System.Collections.Generic.IEnumerable'1[[FHH.crubmApi.Entities.crumb,FHH.crumbapi]]
    }
}

; Please look at the commented line within the code that explains the issue.
In addition, I need to get the object in order to do a compare Assert.Equal(2, item.tagID).
If I fix the line with the issue, I would assign that to a variable called actionResult and then this assigned it to var item = actionResult.firstOrDefault() in order to get item.tagID. I am following the following sample on this website for IEnumerable unit test. Thank you for any feedback.

Look Sample Link

  • 3
    This happens because the return type on your method is `ActionResult>`, not `IEnumerable`. – Stormhashe Feb 20 '19 at 15:32
  • Possible duplicate of [How to get the Model from an ActionResult?](https://stackoverflow.com/questions/2071095/how-to-get-the-model-from-an-actionresult) – Liam Feb 21 '19 at 15:43
  • So @Stormhashe what is the solution of ActionResult> then ? Cause I have obviously noticed this, but never find a solution. – Umar3x Apr 17 '20 at 17:23
  • @Umar3x var test = new WhateverController().WhateverMethod().Result; this gets you the inner result, not the action itself. – Stormhashe Jun 15 '20 at 23:00

1 Answers1

0

You need to get the model value of your ActionResult to do what you intended. You can accomplish that by casting your ActionResult to a ViewResult and getting the model value. Please refer to the accepted answer of this question for more details.

Stormhashe
  • 704
  • 6
  • 16