【发布时间】:2017-03-30 20:32:32
【问题描述】:
我正在准备我的测试基础架构,但我的测试存储库遇到了一些问题。
真正的仓库访问EntityFramework DbSet,像这样:
public class Repository<T>: IRepository<T> where T : ModelBase
{
protected ApplicationDbContext db;
private DbSet<T> dbSet;
public Repository(ApplicationDbContext db)
{
this.db = db;
dbSet = db.Set<T>();
}
public IQueryable<T> Where(Expression<Func<T, bool>> predicate)
{
return dbSet.Where(predicate).AsNoTracking();
}
....
我的 TestRepository 使用 List 而不是 DbSet:
public class TestRepository<T> : IRepository<T> where T : ModelBase
{
private readonly List<T> dbSet;
protected ApplicationDbContextFake db;
public TestRepository(ApplicationDbContextFake db)
{
this.db = db;
this.dbSet = db.Set<T>();
}
这个db.Set<T>() 返回一个列表
测试我的代码时出现问题,有这样的:
public async Task DeleteAsync()
{
var items = repository.Where(....);
repository.RemoveRange(await items.ToListAsync());
此代码使用 Entity DbSets 运行正常,但在使用我的 TestRepository 进行测试时抛出异常:
源 IQueryable 未实现 IAsyncEnumerable。只有实现 IAsyncEnumerable 的源才能用于实体框架异步操作。
有解决此问题的建议吗?
【问题讨论】:
标签: c# asp.net-core xunit.net