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

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

任何帮助是极大的赞赏 !!

最佳答案

您要做的就是围绕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();
        }
    }

现在,您的模拟返回EnumerableQuery的地方,您将返回此MockEnumerableQuery类,您应该会很好。

关于linq - 无法转换类型为 'System.Linq.EnumerableQuery to type '的对象Microsoft.Azure.Documents.Linq.IDocumentQuery,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43563690/

10-09 22:26