IDbAsyncEnumerable not implemented

前端 未结 7 673
清歌不尽
清歌不尽 2020-12-01 10:50

I am trying to make a FakeDbContext with a FakeDbSet for unit testing.

But I get the following error (see below). I am extending DbSet so normally IDbAsyncEnumerable

相关标签:
7条回答
  • 2020-12-01 11:23

    I renamed the example test classes from here to remove the word Test since they are useful outside of testing:

    • DbAsyncEnumerable
    • DbAsyncEnumerator<T>
    • DbAsyncQueryProvider<TEntity>

    Then I added the extension class below so you can now just do ...

    var data = new List<Blog> 
    { 
        new Blog { Name = "BBB" }, 
        new Blog { Name = "ZZZ" }, 
        new Blog { Name = "AAA" }, 
    }.AsAsyncQueryable();   // <<== new extension method
    

    This is not only useful in unit tests but also when you want to implement an IQueryable<T> interface that either returns an async database query or in memory data that you can subsequently safely call as query.ToAsyncArray().

    public static class AsyncQueryableExtensions
    {
        public static IQueryable<TElement> AsAsyncQueryable<TElement>(this IEnumerable<TElement> source)
        {
            return new DbAsyncEnumerable<TElement>(source);
        }
    
        public static IDbAsyncEnumerable<TElement> AsDbAsyncEnumerable<TElement>(this IEnumerable<TElement> source)
        {
            return new DbAsyncEnumerable<TElement>(source);
        }
    
        public static EnumerableQuery<TElement> AsAsyncEnumerableQuery<TElement>(this IEnumerable<TElement> source)
        {
            return new DbAsyncEnumerable<TElement>(source);
        }
    
        public static IQueryable<TElement> AsAsyncQueryable<TElement>(this Expression expression)
        {
            return new DbAsyncEnumerable<TElement>(expression);
        }
    
        public static IDbAsyncEnumerable<TElement> AsDbAsyncEnumerable<TElement>(this Expression expression)
        {
            return new DbAsyncEnumerable<TElement>(expression);
        }
    
        public static EnumerableQuery<TElement> AsAsyncEnumerableQuery<TElement>(this Expression expression)
        {
            return new DbAsyncEnumerable<TElement>(expression);
        }
    }
    
    0 讨论(0)
提交回复
热议问题