Mocking out nHibernate QueryOver with Moq

前端 未结 5 2026
终归单人心
终归单人心 2021-01-04 20:41

The following line fails with a null reference, when testing:

var awards = _session.QueryOver().Where(x => x.BusinessId == (int)business).List         


        
5条回答
  •  情歌与酒
    2021-01-04 21:03

    I've used several approaches in the past. One way is, as others have suggested, to create a Repository class that you can mock/stub that encapsulates your queries. One problem with this is that it's not very flexible and you wind up having a stored procedure like solution, except this one is in code rather than the database.

    A recent solution I have tried is creating a QueryOver stub that I provide when I stub the QueryOver method. I can then provide a list of items that should be returned. Keep in mind if you using this approach you should not only write unit tests, but an integration test, which will test whether or not the query actually works.

    public class QueryOverStub : IQueryOver
    {
        private readonly TRoot _singleOrDefault;
        private readonly IList _list;
        private readonly ICriteria _root = MockRepository.GenerateStub();
    
        public QueryOverStub(IList list)
        {
            _list = list;
        }
    
        public QueryOverStub(TRoot singleOrDefault)
        {
            _singleOrDefault = singleOrDefault;
        }
    
        public ICriteria UnderlyingCriteria
        {
            get { return _root; }
        }
    
        public ICriteria RootCriteria
        {
            get { return _root; }
        }
    
        public IList List()
        {
            return _list;
        }
    
        public IList List()
        {
            throw new NotImplementedException();
        }
    
        public IQueryOver ToRowCountQuery()
        {
            throw new NotImplementedException();
        }
    
        public IQueryOver ToRowCountInt64Query()
        {
            throw new NotImplementedException();
        }
    
        public int RowCount()
        {
            return _list.Count;
        }
    
        public long RowCountInt64()
        {
            throw new NotImplementedException();
        }
    
        public TRoot SingleOrDefault()
        {
            return _singleOrDefault;
        }
    
        public U SingleOrDefault()
        {
            throw new NotImplementedException();
        }
    
        public IEnumerable Future()
        {
            return _list;
        }
    
        public IEnumerable Future()
        {
            throw new NotImplementedException();
        }
    
        public IFutureValue FutureValue()
        {
            throw new NotImplementedException();
        }
    
        public IFutureValue FutureValue()
        {
            throw new NotImplementedException();
        }
    
        public IQueryOver Clone()
        {
            throw new NotImplementedException();
        }
    
        public IQueryOver ClearOrders()
        {
            return this;
        }
    
        public IQueryOver Skip(int firstResult)
        {
            return this;
        }
    
        public IQueryOver Take(int maxResults)
        {
            return this;
        }
    
        public IQueryOver Cacheable()
        {
            return this;
        }
    
        public IQueryOver CacheMode(CacheMode cacheMode)
        {
            return this;
        }
    
        public IQueryOver CacheRegion(string cacheRegion)
        {
            return this;
        }
    
        public IQueryOver And(Expression> expression)
        {
            return this;
        }
    
        public IQueryOver And(Expression> expression)
        {
            return this;
        }
    
        public IQueryOver And(ICriterion expression)
        {
            return this;
        }
    
        public IQueryOver AndNot(Expression> expression)
        {
            return this;
        }
    
        public IQueryOver AndNot(Expression> expression)
        {
            return this;
        }
    
        public IQueryOverRestrictionBuilder AndRestrictionOn(Expression> expression)
        {
            throw new NotImplementedException();
        }
    
        public IQueryOverRestrictionBuilder AndRestrictionOn(Expression> expression)
        {
            throw new NotImplementedException();
        }
    
        public IQueryOver Where(Expression> expression)
        {
            return this;
        }
    
        public IQueryOver Where(Expression> expression)
        {
            return this;
        }
    
        public IQueryOver Where(ICriterion expression)
        {
            return this;
        }
    
        public IQueryOver WhereNot(Expression> expression)
        {
            return this;
        }
    
        public IQueryOver WhereNot(Expression> expression)
        {
            return this;
        }
    
        public IQueryOverRestrictionBuilder WhereRestrictionOn(Expression> expression)
        {
            return new IQueryOverRestrictionBuilder(this, "prop");
        }
    
        public IQueryOverRestrictionBuilder WhereRestrictionOn(Expression> expression)
        {
            return new IQueryOverRestrictionBuilder(this, "prop");
        }
    
        public IQueryOver Select(params Expression>[] projections)
        {
            return this;
        }
    
        public IQueryOver Select(params IProjection[] projections)
        {
            return this;
        }
    
        public IQueryOver SelectList(Func, QueryOverProjectionBuilder> list)
        {
            return this;
        }
    
        public IQueryOverOrderBuilder OrderBy(Expression> path)
        {
            return new IQueryOverOrderBuilder(this, path);
        }
    
        public IQueryOverOrderBuilder OrderBy(Expression> path)
        {
            return new IQueryOverOrderBuilder(this, path, false);
        }
    
        public IQueryOverOrderBuilder OrderBy(IProjection projection)
        {
            return new IQueryOverOrderBuilder(this, projection);
        }
    
        public IQueryOverOrderBuilder OrderByAlias(Expression> path)
        {
            return new IQueryOverOrderBuilder(this, path, true);
        }
    
        public IQueryOverOrderBuilder ThenBy(Expression> path)
        {
            return new IQueryOverOrderBuilder(this, path);
        }
    
        public IQueryOverOrderBuilder ThenBy(Expression> path)
        {
            return new IQueryOverOrderBuilder(this, path, false);
        }
    
        public IQueryOverOrderBuilder ThenBy(IProjection projection)
        {
            return new IQueryOverOrderBuilder(this, projection);
        }
    
        public IQueryOverOrderBuilder ThenByAlias(Expression> path)
        {
            return new IQueryOverOrderBuilder(this, path, true);
        }
    
        public IQueryOver TransformUsing(IResultTransformer resultTransformer)
        {
            return this;
        }
    
        public IQueryOverFetchBuilder Fetch(Expression> path)
        {
            return new IQueryOverFetchBuilder(this, path);
        }
    
        public IQueryOverLockBuilder Lock()
        {
            throw new NotImplementedException();
        }
    
        public IQueryOverLockBuilder Lock(Expression> alias)
        {
            throw new NotImplementedException();
        }
    
        public IQueryOver JoinQueryOver(Expression> path)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression> path)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression> path, Expression> alias)
        {
            return new QueryOverStub(_list);
        }
    
        public IQueryOver JoinQueryOver(Expression> path, Expression> alias)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression> path, JoinType joinType)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression> path, JoinType joinType)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression> path, Expression> alias, JoinType joinType)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression> path, Expression> alias, JoinType joinType)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression>> path)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression>> path)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression>> path, Expression> alias)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression>> path, Expression> alias)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression>> path, JoinType joinType)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression>> path, JoinType joinType)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression>> path, Expression> alias, JoinType joinType)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinQueryOver(Expression>> path, Expression> alias, JoinType joinType)
        {
            return new QueryOverStub(new List());
        }
    
        public IQueryOver JoinAlias(Expression> path, Expression> alias)
        {
            return this;
        }
    
        public IQueryOver JoinAlias(Expression> path, Expression> alias)
        {
            return this;
        }
    
        public IQueryOver JoinAlias(Expression> path, Expression> alias, JoinType joinType)
        {
            return this;
        }
    
        public IQueryOver JoinAlias(Expression> path, Expression> alias, JoinType joinType)
        {
            return this;
        }
    
        public IQueryOverSubqueryBuilder WithSubquery
        {
            get { return new IQueryOverSubqueryBuilder(this); }
        }
    
        public IQueryOverJoinBuilder Inner
        {
            get { return new IQueryOverJoinBuilder(this, JoinType.InnerJoin); }
        }
    
        public IQueryOverJoinBuilder Left
        {
            get { return new IQueryOverJoinBuilder(this, JoinType.LeftOuterJoin); }
        }
    
        public IQueryOverJoinBuilder Right
        {
            get { return new IQueryOverJoinBuilder(this, JoinType.RightOuterJoin); }
        }
    
        public IQueryOverJoinBuilder Full
        {
            get { return new IQueryOverJoinBuilder(this, JoinType.FullJoin); }
        }
    }
    

提交回复
热议问题