title: ASP.Net Web API 2 Controller Unit Test Get Request Item Count
body: <p>I am trying to develop an Xunit test to establish if my Controller under test is returning the correct number of objects.</p>
  
  <p>The Controller's <code>getAreas</code> function is as follows:</p>
  
  <pre><code>[HttpGet()]
  public IActionResult GetAreas()
  {
      _logger.LogTrace("AreasController.GetAreas called.");
  
      try
      {
          // Create an IEnumerable of Area objects by calling the repository.
          var areasFromRepo = _areaRepository.GetAreas();
  
          var areas = _mapper.Map&lt;IEnumerable&lt;AreaDto&gt;&gt;(areasFromRepo);
  
          // Return a code 200 'OK' along with an IEnumerable of AreaDto objects mapped from the Area entities.
          return Ok(areas);
  
      }
      catch (Exception ex)
      {
          _logger.LogError($"Failed to get all Areas: {ex}");
  
          return StatusCode(500, "An unexpected error occurred. Please try again later.");
      }
  
  }
  </code></pre>
  
  <p>My test class uses Moq to mock the Logger, Repository and AutoMapper. I have created a variable to hold a list of objects to be returned by my mock repository:</p>
  
  <pre><code>private List&lt;Area&gt; testAreas = new List&lt;Area&gt;()
  {
      new Area
      {
          Id = new Guid("87d8f755-ef60-4cfa-9a4a-c94cff9f8a22"),
          Description = "Buffer Store",
          SortIndex = 1
      },
      new Area
      {
          Id = new Guid("19952c5a-b762-4937-a613-6151c8cd9332"),
          Description = "Fuelling Machine",
          SortIndex = 2
      },
      new Area
      {
          Id = new Guid("87c7e1d8-1ce7-4d8b-965d-5c44338461dd"),
          Description = "Ponds",
          SortIndex = 3
      }
  };
  </code></pre>
  
  <p>I created my test as follows:</p>
  
  <pre><code>[Fact]
  public void ReturnAreasForGetAreas()
  {
      //Arrange
      var _mockAreaRepository = new Mock&lt;IAreaRepository&gt;();
      _mockAreaRepository
          .Setup(x =&gt; x.GetAreas())
          .Returns(testAreas);
  
      var _mockMapper = new Mock&lt;IMapper&gt;();
  
      var _mockLogger = new Mock&lt;ILogger&lt;AreasController&gt;&gt;();
      var _sut = new AreasController(_mockAreaRepository.Object, _mockLogger.Object, _mockMapper.Object);
  
      // Act
      var result = _sut.GetAreas();
  
      // Assert
      Assert.NotNull(result);
      var objectResult = Assert.IsType&lt;OkObjectResult&gt;(result);
      var model = Assert.IsAssignableFrom&lt;IEnumerable&lt;AreaDto&gt;&gt;(objectResult.Value);
      var modelCount = model.Count();
      Assert.Equal(3, modelCount);
  }
  </code></pre>
  
  <p>The test fails on the final <code>Assert</code>, it gets 0 when expecting 3.</p>
  
  <p>The <code>result</code> is not <code>null</code>. The <code>objectResult</code> is an <code>OkObjectResult</code>. The <code>model</code> is an <code>IEnumerable&lt;AreaDto&gt;</code>, however it contains 0 items in the collection.</p>
  
  <p>I can't see where I am going wrong here. Do I have to configure the mocked Automapper mapping?</p>