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. 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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.