源 IQueryable 不实现 IDbAsyncEnumerable。只有实现 IDbAsyncEnumerable 的源才能用于实体框架异步操作。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=287068。
我是 NUnit 的新手。我为异步查询开发了 Nunit 测试用例。但我正在克服错误。我引用了https://msdn.microsoft.com/en-us/data/dn314429.aspx链接但无法解决错误。
任何形式的帮助将不胜感激!提前致谢!
公共接口 IWork : IDisposable { IBlogRepository BlogRepository { get; }
IDBContext Db { get; }
}
public class BlogRepository : GenericRepository<Blog>, IBlogRepository
{
internal ContainerRepository(IWork work)
: base(work)
{
}
}
public interface IBlogRepository : IGenericRepository<Blog>
{
}
[TestFixture]
public class AsyncQueryTests
{
[Test]
public async Task GetAllBlogsAsync_orders_by_name()
{
using (var work = ObjectFactory.GetInstance<IWork>())
{
var data = new List<Blog>
{
new Blog { Name = "BBB" },
new Blog { Name = "ZZZ" },
new Blog { Name = "AAA" },
}.AsQueryable();
var mockSet = new Mock<DbSet<Blog>>();
mockSet.As<IDbAsyncEnumerable<Blog>>()
.Setup(m => m.GetAsyncEnumerator())
.Returns(new TestDbAsyncEnumerator<Blog>(data.GetEnumerator()));
mockSet.As<IQueryable<Blog>>()
.Setup(m => m.Provider)
.Returns(new TestDbAsyncQueryProvider<Blog>(data.Provider));
mockSet.As<IQueryable<Blog>>().Setup(m => m.Expression).Returns(data.Expression);
mockSet.As<IQueryable<Blog>>().Setup(m => m.ElementType).Returns(data.ElementType);
mockSet.As<IQueryable<Blog>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator());
var mockContext = new Mock<IWork>();
mockContext.Setup(c => c.BlogRepository.GetQuery()).Returns(mockSet.Object);
ObjectFactory.Configure(cfg => cfg.For<IWork>().Use(mockContext.Object));
foreach (var blog in mockSet.Object)
{
mockContext.Object.BlogRepository.Add(blog);
}
var service = new BlogController();
var blogs = await service.GetAllBlogsAsync();
Assert.AreEqual(3, blogs.Count);
}
}
}