title: unit testing with moq generic repository and UnitOfWork
body: <p>I have a specific and generic repository like that :</p>
  
  <p><strong>Generic repository:</strong> </p>
  
  <pre><code>public class Repository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class
  {
      protected readonly DbContext Context;
      private DbSet&lt;TEntity&gt; _entities;
  
      public Repository(DbContext context)
      {
          Context = context;
          _entities = Context.Set&lt;TEntity&gt;();
      }
  
      public IEnumerable&lt;TEntity&gt; Find(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate)
      {
          return _entities.Where(predicate);
      }
  }
  </code></pre>
  
  <p><strong>Specific Repository:</strong></p>
  
  <pre><code>public class HolidayCalendarRepository : Repository&lt;HolidayCalendar&gt;, IHolidayCalendarRepository
  {
      public HolidayCalendarRepository(ApplicationDbContext context)
          : base(context)
      {
      }
  
      public ApplicationDbContext ApplicationDbContext
      {
          get { return Context as ApplicationDbContext; }
      }
  
      public bool IsHoliday(DateTime date)
      {
          return Find(h =&gt; h.Date == date.Date).Any();
      }
  }
  </code></pre>
  
  <p><strong><em>IRepository</em></strong></p>
  
  <pre><code>public interface IRepository&lt;TEntity&gt; where TEntity : class
  {       
      IEnumerable&lt;TEntity&gt; Find(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate);
  }
  </code></pre>
  
  <p><strong><em>IHolidayCalendarRepository</em></strong> </p>
  
  <pre><code>public interface IHolidayCalendarRepository : IRepository&lt;HolidayCalendar&gt;
  {
      bool IsHoliday(DateTime date);                     
  }
  </code></pre>
  
  <p><strong>UnitOfWork</strong></p>
  
  <pre><code>public class UnitOfWork : IUnitOfWork
  {
      private readonly ApplicationDbContext _context;
  
      public UnitOfWork(ApplicationDbContext context)
      {
          _context = context;          
          HolidayCalendars = new HolidayCalendarRepository(_context);
      }
  
      public IHolidayCalendarRepository HolidayCalendars { get; private set; }
  
      public int Complete()
      {
          return _context.SaveChanges();
      }
  
      public void Dispose()
      {
          _context.Dispose();
      }
  }
  </code></pre>
  
  <p>How to test the method <em>IsHoliday(DateTime date)</em> on my respository.
  I'm beginner on unit test. this is what i try but it's not working.</p>
  
  <pre><code>[TestFixture]
  public class HolidayCalendarRepositoryTests
  {
      private HolidayCalendarRepository _holidayCalendarRepository;
      private Mock&lt;DbSet&lt;HolidayCalendar&gt;&gt; _mockHolidayCalendar;
  
      [SetUp]
      public void TestInitialize()
      {          
          var mockContext = new Mock&lt;ApplicationDbContext&gt;();
          _mockHolidayCalendar = new Mock&lt;DbSet&lt;HolidayCalendar&gt;&gt;();
  
  
          mockContext.Setup(hc =&gt; hc.HolidayCalendar).Returns(_mockHolidayCalendar.Object);
          _holidayCalendarRepository = new HolidayCalendarRepository(mockContext.Object);
  
      }
  
      [Test]
      public void IsHoliday_CurrentDateIsHoliday_ShouldBeTrue()
      {
          var holidays = new List&lt;HolidayCalendar&gt;() { new HolidayCalendar { ID = 1, Date = DateTime.Today } };
  
          _mockHolidayCalendar.SetSource(holidays);
  
          var result = _holidayCalendarRepository.IsHoliday(DateTime.Today);
  
          Assert.IsTrue(result);
      }
  }
  </code></pre>
  
  <p><em>SetSource methode</em></p>
  
  <pre><code>    public static void SetSource&lt;T&gt;(this Mock&lt;DbSet&lt;T&gt;&gt; mockSet, IList&lt;T&gt; source) where T : class
      {
          var data = source.AsQueryable();
  
          mockSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Provider).Returns(data.Provider);
          mockSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Expression).Returns(data.Expression);
          mockSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(data.ElementType);
          mockSet.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(data.GetEnumerator());
      }
  </code></pre>