无法将类型为System.Linq.EnumerableQuery的对象强制转换为' Microsoft.Azure.Documents.Linq.IDocumentQuery

时间:2017-04-22 19:29:16

标签: linq azure

我有一个使用以下方法的类,并使用Moq作为单元测试框架。我该如何嘲笑以下内容:

FeedOptions feedOptions = new FeedOptions
        {
            MaxItemCount = 1000
        };

        var query = await _storeAccessClient.CreateDocumentQueryAsync<CustomEntity>(_collectionLink, feedOptions)
            .Where(c => c.DataType == _dataType)
            .OrderBy(c => c.StartTime, sortOrder)
            .AsDocumentQuery()
        .ExecuteNextAsync<CustomEntity>();

        List<CustomEntity> result = query.ToList<CustomEntity>();

任何帮助都会受到极大的赞赏!!

1 个答案:

答案 0 :(得分:4)

你要做的就是创建EnumerableQuery类的包装器,它继承自IQueryable和IDocumentQuery,如下所示:

internal class MockEnumerableQuery : IDocumentQuery<JTokenEx>, IOrderedQueryable<JTokenEx>
{
        public IQueryable<JTokenEx> List;
        private readonly bool bypassExpressions;


        public MockEnumerableQuery(EnumerableQuery<JTokenEx> List, bool bypassExpressions = true)
        {
            this.List = List;
            this.bypassExpressions = bypassExpressions;
        }


        public IEnumerator<JTokenEx> GetEnumerator()
        {
            return List.GetEnumerator();
        }


        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }


        public Expression Expression => List.Expression;

        public Type ElementType => typeof(JTokenEx);


        public IQueryProvider Provider => new MockQueryProvider(this, bypassExpressions);


        public void Dispose()
        {
        }


        public Task<FeedResponse<TResult>> ExecuteNextAsync<TResult>(CancellationToken token = new CancellationToken())
        {
            BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
            FeedResponse<JToken> feed = Activator.CreateInstance(typeof(FeedResponse<JToken>),
                flags,null,new Object[] { List.Select(j => (JToken) j), 0, new NameValueCollection(), false, null}, null) 
                as FeedResponse<JToken>;

            return Task.FromResult(feed as FeedResponse<TResult>);
        }


        public Task<FeedResponse<dynamic>> ExecuteNextAsync(CancellationToken token = new CancellationToken())
        {
            throw new NotImplementedException();
        }


        public bool HasMoreResults { get; }
    }


    class MockQueryProvider : IQueryProvider
    {
        private readonly MockEnumerableQuery mockQuery;
        private readonly bool bypassExpressions;

        public MockQueryProvider(MockEnumerableQuery mockQuery, bool byPassExpressions)
        {
            this.mockQuery = mockQuery;
            this.bypassExpressions = byPassExpressions;
        }


        public IQueryable CreateQuery(Expression expression)
        {
            throw new NotImplementedException();
        }


        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            if (!bypassExpressions)
            {
                mockQuery.List = mockQuery.List.Provider.CreateQuery<TElement>(expression) as IQueryable<JTokenEx>;
            }

            return (IQueryable<TElement>)mockQuery;
        }


        public object Execute(Expression expression)
        {
            throw new NotImplementedException();
        }


        public TResult Execute<TResult>(Expression expression)
        {
            throw new NotImplementedException();
        }
    }

现在你的mock返回EnumerableQuery,你返回这个MockEnumerableQuery类,你应该很好。