如何使用moq模拟这些类?



请有人告诉我如何正确地模拟这个存储库类。基本上,我只是想模拟它,以便我可以轻松地测试我的服务层。在我的测试项目中使用c# moq。我遇到的主要问题是Query、Filter、Include和OrderBy方法中使用的虚拟接口模式。我如何模拟它,以便它可以返回正确的RepositoryQuery,我注入到我的Repository模拟。

下面显示的代码是与数据库通信的数据库层的一部分,所以我想模拟它,这样我就可以轻松地测试我的服务层,而不用担心外部依赖。

 public sealed class RepositoryQuery<TEntity> : IRepositoryQuery<TEntity> where TEntity  : BaseEntity
   {
        private readonly List<Expression<Func<TEntity, object>>> _includeProperties;
        private readonly Repository<TEntity> _repository;
        private readonly List<Expression<Func<TEntity, bool>>> _filters;
        private Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> _orderByQuerable;
        private int? _page;
        private int? _pageSize;
        public RepositoryQuery(Repository<TEntity> repository)
        {
            _repository = repository;
            _includeProperties = new List<Expression<Func<TEntity, object>>>();
            _filters = new List<Expression<Func<TEntity, bool>>>();
        }
        public RepositoryQuery<TEntity> Filter(Expression<Func<TEntity, bool>> filter)
        {
            _filters.Add(filter);
            return this;
        }
        public RepositoryQuery<TEntity> OrderBy(Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy)
        {
            _orderByQuerable = orderBy;
            return this;
        }
        public RepositoryQuery<TEntity> Include(Expression<Func<TEntity, object>> expression)
        {
            _includeProperties.Add(expression);
            return this;
        }    
        public IQueryable<TEntity> Get()
        {
            return _repository.Get(_filters, _orderByQuerable, _includeProperties, _page, _pageSize);
        }
    }

public class Repository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
    {
         private readonly Guid _instanceId;
        private readonly DbSet<TEntity> _dbSet;
        private readonly IDbContext _context; 
        public Repository(IDbContext context)
        {
            _context = context;
            _dbSet = context.Set<TEntity>();
            _instanceId = Guid.NewGuid();
        }
        public virtual IRepositoryQuery<TEntity> Query()
        {
            var repositoryGetFluentHelper = new RepositoryQuery<TEntity>(this);
            return repositoryGetFluentHelper;
        }
        internal IQueryable<TEntity> Get(
            List<Expression<Func<TEntity, bool>>> filters = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            List<Expression<Func<TEntity, object>>> includeProperties = null,
            int? page = null,
            int? pageSize = null)
        {
            IQueryable<TEntity> query = _dbSet;
            if (includeProperties != null)
            {
                includeProperties.ForEach(i => query = query.Include(i));
            }
            if (filters != null && filters.Any())
            {
                query = filters.Aggregate(query, (current, filter) => current.Where(filter));
            }
            query = orderBy != null ? orderBy(query) : query.OrderBy(a => a.Id);
            if (page != null && pageSize != null)
            {
                query = query
                    .Skip((page.Value - 1)*pageSize.Value)
                    .Take(pageSize.Value);
            }
            return query;
        }

    }

希望您的组件依赖于接口IRepositoryQuery<TEntity>IRepository<TEntity>,而不是具体实现RepositoryQuery<TEntity>Repository<TEntity>。如果是这种情况,那么您可以为Moq提供用于单元测试目的的接口的测试double,如

var mockQuery = new Mock<IRepositoryQuery<TEntity>>();
// perform any setup needed on mockQuery for the particular System Under Test
var mockRepository = new Mock<IRepository<TEntity>>();
// perform any setup needed on mockRepository for the particular System Under Test
// component that relies on query and repository
// that is the System Under Test i.e. the focus of the unit test
var systemUnderTest = new SystemUnderTest(mockRepository.Object, mockQuery.Object);

观察;看看RepositoryQuery<TEntity>,我认为这些方法应该返回接口IRepositoryQuery<TEntity>,而不是具体实现RepositoryQuery<TEntity>

如果IRepositoryQueryIRepository的包装器,那么—为了测试您的服务层—您不需要模拟IRepository。模仿IRepositoryQuery

假设我想检查SomeService.DoSomething正确处理它从IRepositoryQuery.Get获得的结果:

var mock = new Mock<IRepositoryQuery<SomeClass>>();
mock.Setup(o => o.Get(/* test parameters */)).Returns(/* result */);
var myService = new SomeService(mock.Object);
Assert.That(myService.DoSomething(), Is.EqualTo(/* expected result*/));

相关内容

  • 没有找到相关文章

最新更新