以下是我的功能

 public async Task<IEnumerable<Books>> GetAsync(Guid customerId) {
        var BookList= await _bookStore.FindBy(AnExpression(customerId)).ToListAsync();
        return vehicleList;
    }


我试图模拟_bookStore.FindBy()方法,该方法返回IQueryable

  var Books= new List<Books>(2){
                new Books(),
                new Books()
            }.AsQueryable();

 mock.Expect(viewStore => viewStore.FindBy(Arg<Expression<Func<Books, bool>>>.Is.Anything)).Return(Books);


viewStore是一个接口:

public interface IRepository<T> : IDisposable where T : class
{
    IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
}


我收到以下错误

System.InvalidOperationException:源IQueryable没有实现IDbAsyncEnumerable。仅实现IDbAsyncEnumerable的源可用于Entity Framework异步操作

我试过像https://msdn.microsoft.com/en-us/data/dn314429#async这样实现AsyncQueryProvider

但是实现是通过Moq而不是Rhino模拟显示的。

最佳答案

您必须使用在您提供的链接中实现的TestDbAsyncEnumerable

更换:

var Books= new List<Books>(2){
            new Books(),
            new Books()
        }.AsQueryable();

mock.Expect(viewStore => viewStore.FindBy(Arg<Expression<Func<Books, bool>>>.Is.Anything))
    .Return(Books);


与:

var Books = new List<Books>(2){
            new Books(),
            new Books()
        }.AsQueryable();

var asycEnumarable = new TestDbAsyncEnumerable<Books>(Books);

var viewStore = MockRepository.GenerateStub<IRepository<Books>>();

viewStore.Stub(x => x.FindBy(Arg<Expression<Func<Books, bool>>>.Is.Anything))
         .Return(asycEnumarable);

10-07 19:39
查看更多