title: Mocking UnitOfWork/GenericRepository with Moq
body: <p>I'm running into an issue where Moq doesn't return what I excpect using following code:</p>
  
  <pre><code>[TestMethod]
  public void GetResultReturnsAResult()
  {
      var mockUnitOfWork = Arrange();
      // Arrange
      var controller = new ResultsController(mockUnitOfWork.Object);
      controller.Request = new HttpRequestMessage();
      controller.Configuration = new HttpConfiguration();
  
      // Act
      var response = controller.GetResult(2);
  
      // Assert
      Assert.IsTrue(response is OkNegotiatedContentResult&lt;Result&gt;);
      var contentResult = response as OkNegotiatedContentResult&lt;Result&gt;;
      Assert.IsNotNull(contentResult);
      Assert.IsNotNull(contentResult.Content);
      Assert.IsTrue(contentResult.Content.ID == 2);
  }
  
  [TestMethod]
  public void GetResultReturnsNotFound()
  {
      var mockUnitOfWork = Arrange();
  
      // Arrange
      var controller = new ResultsController(mockUnitOfWork.Object);
      controller.Request = new HttpRequestMessage();
      controller.Configuration = new HttpConfiguration();
  
      // Act
      var response = controller.GetResult(100);
  
      // Assert
      Assert.IsTrue(response is NotFoundResult);
      var contentResult = response as NotFoundResult;
      Assert.IsNotNull(contentResult);
  }
  
  private Mock&lt;IUnitOfWork&gt; Arrange()
  {
      var results = new List&lt;Result&gt;()
      {
          new Result()
          {
              ID = 1,
              Name = "Result 1",
              Modified = new DateTime(2017, 1, 1),
              Created = new DateTime(2017, 1, 1),
              CreatedBy = "Tester 1",
              ModifiedBy = "Tester 2"
          },
          new Result()
          {
              ID = 2,
              Name = "Result 2",
              Modified = new DateTime(2017, 1, 2),
              Created = new DateTime(2017, 1, 2),
              CreatedBy = "Tester 1",
              ModifiedBy = "Tester 2"
          },
      };
  
      var mockUnitOfWork = new Mock&lt;IUnitOfWork&gt;();
      var mockResultRepository = new Mock&lt;IGenericRepository&lt;Result&gt;&gt;();
  
      mockResultRepository.Setup(x =&gt; x.Get(null, null, ""))
          .Returns(results);
      mockResultRepository.Setup(x =&gt; x.GetById(It.IsAny&lt;int&gt;()))
          .Returns((int id) =&gt; GetById(results, id));
      mockUnitOfWork.Setup(x =&gt; x.ResultRepository)
          .Returns(mockResultRepository.Object);
  
      return mockUnitOfWork;
  }
  
  private Result GetById(List&lt;Result&gt; results, int id)
  {
      return results.FirstOrDefault(r =&gt; r.ID == id);
  }
  </code></pre>
  
  <p>In this case my TestMethods testing GetById both throw an exception with message: Object of type 'System.Object[]' cannot be converted to type 'System.Int32'.</p>
  
  <p>When I change the mocking code to this:</p>
  
  <pre><code>mockResultRepository.Setup(x =&gt; x.Get(null, null, ""))
      .Returns(results);
  mockUnitOfWork.Setup(x =&gt; x.ResultRepository)
      .Returns(mockResultRepository.Object);
  mockUnitOfWork.Setup(x =&gt; x.ResultRepository.GetById(It.IsAny&lt;int&gt;()))
      .Returns((int id) =&gt; GetById(results, id));
  </code></pre>
  
  <p>The GetById tests don't throw an exception but GetResultReturnsAResult doesn't return a result so it always fails.</p>
  
  <p>Can anyone shed a light on this behaviour?</p>