[英]IQueryable as List<T>
I have a working service for retrieving books from a database, for which I am currently doing tests.我有一个从数据库中检索书籍的工作服务,我目前正在为此进行测试。 The service looks like this:
该服务如下所示:
public async Task<List<Book>> GetBooks()
{
........
........
IQueryable results = repository.Query();
var list = await Task.Factory.StartNew(() => results as List<Book>);
return list
}
This code works great when the application is running.此代码在应用程序运行时运行良好。
During the test, I can check with the debugger that the results variable has the value I need.在测试期间,我可以使用调试器检查结果变量是否具有我需要的值。 But list variable has null meaning.
但是列表变量有null 的含义。 Here is the definition of my moq:
这是我的起订量的定义:
var books = new List<Book>();
for (int i = 1; i < 4; i++)
{
books.Add(new Book
{
new Book { Id = i , Author = $"New Author {i}"}
});
}
var mockBookRepository = new Mock<IBookRepository>();
mockBookRepository
.Setup(x => x.Query())
.Returns(books.AsQueryable());
I need to do a test that will return values in a variable list .我需要做一个将返回变量列表中的值的测试。 Changing the GetBooks () method is not valid
更改GetBooks()方法无效
You're trying to do an invalid cast you should be materializing你正在尝试做一个你应该实现的无效演员表
public async Task<Object> GetBooks()
{
//........
return await repository.Query().ToListAsync()
}
PS附言
Make sure repository.Query() returns an IQueryable<Book>
and not just IQueryable
确保 repository.Query() 返回
IQueryable<Book>
而不仅仅是IQueryable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.