简体   繁体   中英

Unit testing using Moq and setting up GetAll with include

I have this strange problem with MOQ and I cant use the GetAll('include') method to test my controller.

My Tests initialization

// GetAll
menusDb.Setup(m => m.GetAll()).Returns(menus.AsQueryable());
menusDb.Setup(m => m.GetAll(It.IsAny<Expression<Func<Menu, object>>>()))
       .Returns((Expression<Func<Menu,object>> pred) => { 
           return menus.AsQueryable();
       });
// FindByIdAsync
menusDb.Setup(m => m.FindByByIdAsync(It.IsAny<int>()))
       .Returns((int x) => Task.FromResult(menus.Find(m => m.ID == x)));
menusDb.Setup(m => m.FindByByIdAsync(It.IsAny<int>(), It.IsAny<Expression<Func<Menu, object>>[]>()))
       .Returns((int x, Expression<Func<Menu,
           object>>[] includeProperties) => Task.FromResult(menus.Find(m => m.ID == x)));

Now whenever I am trying to test

_menusDB.GetAll(s=>s.Sections)

The moq version of menusdb.getAll() method is not fired at all

All other Moq methods are triggered correctly... examples

_menusDB.GetAll();
_menusDB.FindByByIdAsync(id,
                m => m.Sections.Select(s => s.Image),
                m => m.Sections.Select(s => s.MenuItems.Select(mi => mi.Image)));

And these are the Getall and find functions with includes in my generic repository.

public IQueryable<TEntity> GetAll<TProperty>(Expression<Func<TEntity, TProperty>> propertyToInclude) {            
    return ObjectSet.Include(propertyToInclude);
}

public async Task<TEntity> FindByByIdAsync(int id, params Expression<Func<TEntity, object>>[] propertiesToInclude) {            
    var query = propertiesToInclude.Aggregate(ObjectSet as IQueryable<TEntity>, (current, property) => current.Include(property));
    return await query.SingleOrDefaultAsync(entity => entity.ID == id).ConfigureAwait(false);
}

I finally found where is the problem. GetAll with include uses generic TProperty instead of object to include the properties. Mock cant somehow relate the object Tproperty with the linq query i provide in test. Maybe there is a way to make it work but for now i simply changed the generic property to object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM