Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cast object of type 'System.Linq.EnumerableQuery to type 'Microsoft.Azure.Documents.Linq.IDocumentQuery

I have a class with the following Method and am using Moq as a Unit Testing Framework. How can I mock the following:

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

Any Help is greatly Appreciated !!

like image 579
Abdullah Khan Avatar asked Apr 22 '17 19:04

Abdullah Khan


1 Answers

All you have to do is create a wrapper around EnumerableQuery class which inherits from IQueryable and IDocumentQuery like this:

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

Now where your mock is returning EnumerableQuery, you return this MockEnumerableQuery class and you should be good.

like image 196
user1740131 Avatar answered Nov 12 '22 02:11

user1740131