IDbAsyncQueryProvider in EntityFrameworkCore

.net .net-core c# entity-framework entity-framework-core

Question

To test a dot net core program, I'm using XUNIT.

A service that internally does an async query on a DbSet in my datacontext has to be tested.

That DbSet may be asynchronously mocked, I've noticed this.

The issue I'm experiencing is that theIDbAsyncQueryProvider seems to be unavailable in EntityframeworkCore, the software I use.

Am I wrong in this? Has this been successful for anybody else?

(It's been a long day; perhaps, I'm simply overlooking something obvious)

EDIT

I received a pointer to this class after inquiring on GitHub: https://github.com/aspnet/EntityFramework/blob/dev/src/Microsoft.EntityFrameworkCore/Query/Internal/IAsyncQueryProvider.cs

In attempting to put this into practice, I've so far come to the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query.Internal;

namespace EFCoreTestQueryProvider
{
    internal class TestAsyncQueryProvider<TEntity>: IAsyncQueryProvider
    {
        private readonly IQueryProvider _inner;

        internal TestAsyncQueryProvider(IQueryProvider inner)
        {
            _inner = inner;
        }

        IQueryable CreateQuery(Expression expression)
        {
            return new TestDbAsyncEnumerable<TEntity>(expression);
        }

        IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
             return new TestDbAsyncEnumerable<TElement>(expression);
        }

        object Execute(Expression expression)
        {
            return _inner.Execute(expression);
        }

        TResult Execute<TResult>(Expression expression)
        {
            return _inner.Execute<TResult>(expression);
        }

        IAsyncEnumerable<TResult> ExecuteAsync<TResult>(Expression expression)
        {
            return Task.FromResult(Execute<TResult>(expression)).ToAsyncEnumerable();
        }

        Task<TResult> IAsyncQueryProvider.ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
        {
            return Task.FromResult(Execute<TResult>(expression));
        }
    }

    internal class TestDbAsyncEnumerable<T> : EnumerableQuery<T>, System.Collections.Generic.IAsyncEnumerable<T>, IQueryable<T>
    {
        public TestDbAsyncEnumerable(IEnumerable<T> enumerable)
            : base(enumerable)
        { }

        public TestDbAsyncEnumerable(Expression expression)
            : base(expression)
        { }

        public IAsyncEnumerator<T> GetAsyncEnumerator()
        {
            return new TestDbAsyncEnumerable<T>(this.AsEnumerable()).ToAsyncEnumerable();
        }

        IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()
        {
            return GetAsyncEnumerator();
        }

        IAsyncEnumerator<T> IAsyncEnumerable<T>.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        IQueryProvider IQueryable.Provider
        {
            get { return new TestAsyncQueryProvider<T>(this); }
        }
    }
}

Now that I've attempted to do this, I've encountered additional problems, particularly with these two techniques:

public IAsyncEnumerator<T> GetAsyncEnumerator()
{
    return new TestDbAsyncEnumerable<T>(this.AsEnumerable()).ToAsyncEnumerable();
}

IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()
{
    return GetAsyncEnumerator();
}

I'm hoping someone can help me figure out what I'm doing incorrectly and lead me in the proper route.

1
11
9/30/2016 12:31:35 PM

Popular Answer

ZZZ_tmp
16
4/24/2017 5:42:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow