title: Mock UnitOfWork interface with a Repository interface property
body: <p>I am implementing a unit test to test a method in a helper class.<br>
  I have mocked my IRepository interface and passed its object to my mocked IUnitOfWork because this IRepository interface is a property to the UnitOfWork. While debugging the unit test, I found out that the call to '_unitOfWork.Booking.OverlappingBooking' in the helper class always return null. The OverlappingBooking()is not invoked at all. </p>
  
  <pre><code>public class BookingHelper
      {
          private IUnitOfWork _unitOfWork;
  
          public BookingHelper(IUnitOfWork unitOfWork)
          {
              _unitOfWork = unitOfWork;
          }
  
          public string OverlappingBookingsExist(Booking booking)
          {
              if (booking.Status == "Cancelled")
                  return string.Empty;
  
              var bookings = _unitOfWork.Booking.GetActiveBookings&lt;Booking&gt;().Where(
                          b =&gt; b.Id != booking.Id &amp;&amp; b.Status != "Cancelled"); 
  
              var overlappingBooking = _unitOfWork.Booking.OverlappingBooking(bookings, booking);
  
  
              return overlappingBooking;
          }
      }
  </code></pre>
  
  <p>my UnitOfWork</p>
  
  <pre><code> public interface IUnitOfWork
      {
          IBookingHelperRepository Booking { get; set; }
      }
      public class UnitOfWork: IUnitOfWork
      {
  
          public IBookingHelperRepository Booking { get; set; }
  
          public UnitOfWork()
          {
              Booking = new BookingHelperRepository();
          }
  
      }
  </code></pre>
  
  <p>Repository</p>
  
  <pre><code>public interface IBookingHelperRepository
      {
          IQueryable&lt;T&gt; GetActiveBookings&lt;T&gt;();
  
          string OverlappingBooking(IQueryable&lt;Booking&gt; bookings, Booking booking);
      }
  
  
      public class BookingHelperRepository: IBookingHelperRepository
      {
          public IQueryable&lt;T&gt; GetActiveBookings&lt;T&gt;()
          {
              var resp = new List&lt;T&gt;().AsQueryable();
  
              return resp;
          }
  
          public string OverlappingBooking(IQueryable&lt;Booking&gt; bookings, Booking booking)
          {
            var overlappingBooking =  bookings.FirstOrDefault(
                      b =&gt; booking.ArrivalDate &gt;= b.ArrivalDate
                          &amp;&amp; booking.ArrivalDate &lt; b.DepartureDate
                          || booking.DepartureDate &gt; b.ArrivalDate
                          &amp;&amp; booking.DepartureDate &lt;= b.DepartureDate);
  
              return overlappingBooking == null ? string.Empty : overlappingBooking.Reference;
          }
      }
  </code></pre>
  
  <p>I have set up my test class as below. </p>
  
  <pre><code> [TestClass]
      public class BookingHelperTests
      {
   private Mock&lt;IBookingHelperRepository&gt; bookingHelperRepository;
   private BookingHelper bookingHelper;
   private Booking booking;
   private Mock&lt;IUnitOfWork&gt; unitOfWork;
   [TestInitialize]
          public void Initialise()
          {
              unitOfWork = new Mock&lt;IUnitOfWork&gt;();
              bookingHelper = new BookingHelper(unitOfWork.Object);
              bookingHelperRepository = new Mock&lt;IBookingHelperRepository&gt;();
              .....
          }
  
   [TestMethod]
          public void OverlappingBookingsExist_BookingStartsAndFinishesBeforeAnExistingBooking_ShouldReturnEmptyString()
          {
              bookingHelperRepository.Setup(y =&gt; y.GetActiveBookings&lt;Booking&gt;()).Returns(bookingListBefore.AsQueryable());
              unitOfWork.Setup(x =&gt; x.Booking).Returns(bookingHelperRepository.Object);
  
              //Act
              var result = bookingHelper.OverlappingBookingsExist(booking);
  
              //Assert
              Assert.AreEqual("", result);
          }
  </code></pre>