Omar AMEZOUG
Omar AMEZOUG

Reputation: 998

unit testing with moq generic repository and UnitOfWork

I have a specific and generic repository like that :

Generic repository:

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected readonly DbContext Context;
    private DbSet<TEntity> _entities;

    public Repository(DbContext context)
    {
        Context = context;
        _entities = Context.Set<TEntity>();
    }

    public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
    {
        return _entities.Where(predicate);
    }
}

Specific Repository:

public class HolidayCalendarRepository : Repository<HolidayCalendar>, IHolidayCalendarRepository
{
    public HolidayCalendarRepository(ApplicationDbContext context)
        : base(context)
    {
    }

    public ApplicationDbContext ApplicationDbContext
    {
        get { return Context as ApplicationDbContext; }
    }

    public bool IsHoliday(DateTime date)
    {
        return Find(h => h.Date == date.Date).Any();
    }
}

IRepository

public interface IRepository<TEntity> where TEntity : class
{       
    IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
}

IHolidayCalendarRepository

public interface IHolidayCalendarRepository : IRepository<HolidayCalendar>
{
    bool IsHoliday(DateTime date);                     
}

UnitOfWork

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();
    }
}

How to test the method IsHoliday(DateTime date) on my respository. I'm beginner on unit test. this is what i try but it's not working.

[TestFixture]
public class HolidayCalendarRepositoryTests
{
    private HolidayCalendarRepository _holidayCalendarRepository;
    private Mock<DbSet<HolidayCalendar>> _mockHolidayCalendar;

    [SetUp]
    public void TestInitialize()
    {          
        var mockContext = new Mock<ApplicationDbContext>();
        _mockHolidayCalendar = new Mock<DbSet<HolidayCalendar>>();


        mockContext.Setup(hc => hc.HolidayCalendar).Returns(_mockHolidayCalendar.Object);
        _holidayCalendarRepository = new HolidayCalendarRepository(mockContext.Object);

    }

    [Test]
    public void IsHoliday_CurrentDateIsHoliday_ShouldBeTrue()
    {
        var holidays = new List<HolidayCalendar>() { new HolidayCalendar { ID = 1, Date = DateTime.Today } };

        _mockHolidayCalendar.SetSource(holidays);

        var result = _holidayCalendarRepository.IsHoliday(DateTime.Today);

        Assert.IsTrue(result);
    }
}

SetSource methode

    public static void SetSource<T>(this Mock<DbSet<T>> mockSet, IList<T> source) where T : class
    {
        var data = source.AsQueryable();

        mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider);
        mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression);
        mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType);
        mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
    }

Upvotes: 3

Views: 1702

Answers (1)

Nkosi
Nkosi

Reputation: 247213

Based on past experiences it looks like the setup of the context was not done correctly.

The Repository is accessing DbSets via the .Set<TEntity>() method...

Generic repository:

//...ctor
public Repository(DbContext context) {
    Context = context;
    _entities = Context.Set<TEntity>(); //<-- Note method used to access DbSet
}
//...

but the mock was setup via the exposed property HolidayCalendar and not the .Set<TEntity>()

HolidayCalendarRepositoryTests

mockContext
    .Setup(hc => hc.HolidayCalendar) //<-- Note the setup
    .Returns(_mockHolidayCalendar.Object);

That needs to be set up to the expected behavior of the code being used.

Update the setup to include...

//...
mockContext
    .Setup(_ => _.Set<HolidayCalendar>())
    .Returns(_mockHolidayCalendar.Object);
//...

...that would allow the repository implementation to behave as expected when the test is being exercised.

The assumption here is that _mockHolidayCalendar.SetSource(holidays) is some sort of extension method that assigns an enumerable/queriable fake data source to the mock collection.

Upvotes: 2

Related Questions