title: Setting up Mock repository with Moq
body: <p>I am unit testing controller logic with xUnit and Moq.  I am trying to use a Mock repository object which will enable the test case to pass.  However, in my setup it caused the test to fail because the Mock repository returns <code>null</code> instead of a <code>TweetDay</code> object.</p>
  
  <p>I am testing the TweetsController:</p>
  
  <pre><code>public class TweetsController : ControllerBase
  {
      private readonly ITweetDayRepository _tweetDayRepository;
  
      public TweetsController(ITweetDayRepository tweetDayRepository)
      {
          //Implementation here
      }
  
      public async Task&lt;IActionResult&gt; GetTweetDay()
      {
              var tweet = await _tweetDayRepository.GetTweetOfTheDayAsync(_systemClock.GetToday);
  
              if (tweet == null)
              {
                  _logger.LogError(LoggingEvents.GetItemNotFound, "An error occurred getting tweet with date: {Date}", _systemClock.GetToday);
                  return BadRequest();
              }
  
              return Ok(tweet);
          }
          catch (Exception ex)
          {
              _logger.LogError(LoggingEvents.GetItemNotFound, ex, "An error occurred getting tweet with date: {Date}", _systemClock.GetToday);
              return BadRequest();
          }
      }
  }
  </code></pre>
  
  <p>Below is my test case:</p>
  
  <pre><code>public class TweetsControllerTests
  {
      private Mock&lt;IMemoryCache&gt; _cache;
      private readonly Mock&lt;IMapper&gt; _mapper;
      private readonly Mock&lt;ILogger&lt;TweetsController&gt;&gt; _logger;
      private readonly Mock&lt;ISystemClockService&gt; _systemClock;
      //private readonly ITweetDayRepository _tweetDayRepository;
  
      public TweetsControllerTests()
      {
          _cache = new Mock&lt;IMemoryCache&gt;();
          _mapper = new Mock&lt;IMapper&gt;();
          _systemClock = new Mock&lt;ISystemClockService&gt;();
          _logger = new Mock&lt;ILogger&lt;TweetsController&gt;&gt;();
      }
  
      [Fact]
      public async Task GetTweetDay_ReturnsOkResult_WithATweetObject()
      {
          // Arrange
          var mockRepo = new Mock&lt;ITweetDayRepository&gt;();
          mockRepo.Setup(repo =&gt; repo.GetTweetOfTheDayAsync(DateTime.Today))
              .ReturnsAsync(GetTestTweet());
  
          var controller = new TweetsController(mockRepo.Object, _cache.Object, _logger.Object,
                                                   _systemClock.Object, _mapper.Object);
  
          // Act
          var result = await controller.GetTweetDay();
  
          // Assert
          var okResult = Assert.IsType&lt;OkResult&gt;(result);
      }
  
      private TweetDay GetTestTweet()
      {
          var tweet = new TweetDay()
          {
              Bird = new Bird(),
              BirdId = 0,
              CreationDate = new DateTime(2016, 7, 2),
              DisplayDay = new DateTime(2016, 7, 2),
              LastUpdateDate = new DateTime(2016, 7, 2),
              TweetDayId = 0
          };
  
          return tweet;
      }
  }
  </code></pre>
  
  <p>Under my setup, I expect the Mock repository to return the TweetDay object intialised in</p>
  
  <pre><code>mockRepo.Setup(repo =&gt; repo.GetTweetOfTheDayAsync(DateTime.Today))
      .ReturnsAsync(GetTestTweet());
  </code></pre>
  
  <p>I cannot isolate the exact source of the problem.  </p>
  
  <p>Am I setting up the Mock repository properly?  Or is the problem elsewhere?</p>