❔ Unit test: The source 'IQueryable' doesn't implement 'IAsyncEnumerable<T>'

My test keep failing when it trying to get to this line of code
var foods = await _foodRepository.FindAll().ToListAsync(cancellationToken);
var foods = await _foodRepository.FindAll().ToListAsync(cancellationToken);
Detail FindAll
IQueryable<T> FindAll(Expression<Func<T, bool>>? predicate = null);
IQueryable<T> FindAll(Expression<Func<T, bool>>? predicate = null);
Controller
public async Task<IActionResult> GetAll(CancellationToken cancellationToken = default)
{
var foods = await _foodRepository.FindAll().ToListAsync(cancellationToken);
return Ok(_mapper.Map<IEnumerable<FoodDTO>>(foods));
}
public async Task<IActionResult> GetAll(CancellationToken cancellationToken = default)
{
var foods = await _foodRepository.FindAll().ToListAsync(cancellationToken);
return Ok(_mapper.Map<IEnumerable<FoodDTO>>(foods));
}
Test
public async Task FoodController_GetAll_ReturnOk()
{
#region[Arrange]
var foods = A.Fake<IEnumerable<Food>>();
var foodsDTO = A.Fake<IEnumerable<FoodDTO>>();
A.CallTo(() => _mapper.Map<IEnumerable<FoodDTO>>(foods)).Returns(foodsDTO);
var controller = new FoodController(_mapper, _foodRepository, _userFoodRepository);
#endregion

#region[Act]
var result = await controller.GetAll();
#endregion

#region[Assert]
result.Should().NotBeNull();
result.Should().BeOfType(typeof(OkObjectResult));
#endregion
}
public async Task FoodController_GetAll_ReturnOk()
{
#region[Arrange]
var foods = A.Fake<IEnumerable<Food>>();
var foodsDTO = A.Fake<IEnumerable<FoodDTO>>();
A.CallTo(() => _mapper.Map<IEnumerable<FoodDTO>>(foods)).Returns(foodsDTO);
var controller = new FoodController(_mapper, _foodRepository, _userFoodRepository);
#endregion

#region[Act]
var result = await controller.GetAll();
#endregion

#region[Assert]
result.Should().NotBeNull();
result.Should().BeOfType(typeof(OkObjectResult));
#endregion
}
22 Replies
phaseshift
phaseshift2y ago
Iqueryable is ienumerable, not the async version
TotechsStrypper
TotechsStrypperOP2y ago
I still don't understand these type can you provide me a fix for this situation ?
phaseshift
phaseshift2y ago
Don't use ToListAsync ToList() should work Although ToList generally not a good idea to trigger on db queries
TotechsStrypper
TotechsStrypperOP2y ago
I don't have permission to change the controller implementation right now do we have any alternative to make the unittest fit for this controller ?
phaseshift
phaseshift2y ago
Maybe you just need to add using System. Data.Entity
PontiacGTX
PontiacGTX2y ago
what the repository implementation probably it fails due to the query?
TotechsStrypper
TotechsStrypperOP2y ago
TotechsStrypper
TotechsStrypperOP2y ago
the mock is not implement the right interface
PontiacGTX
PontiacGTX2y ago
what is where if? there showing you to send the predicate and would do it where isn't null but you didnt send a predicate then it is null? _foodRepository.FindAll() so rather change the implementation for the FindAll repository's Method
Jayy
Jayy2y ago
GitHub
GitHub - romantitov/MockQueryable: Mocking Entity Framework Core op...
Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc - GitHub - romantitov/MockQueryable: Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsyn...
Jayy
Jayy2y ago
??? Good lord y'all lol You have to mock async stuff a little diff
TotechsStrypper
TotechsStrypperOP2y ago
So it cannot face the IQueryable a guy help me create a custom interface var foods = A.Fake<IEFMock>();
public interface IEFMock : IQueryable<Food>, IAsyncEnumerable<Food>
{
}
public interface IEFMock : IQueryable<Food>, IAsyncEnumerable<Food>
{
}
Jayy
Jayy2y ago
This will crash at runtime You have to mock all of IAsyncEnumerable Which the nuget i linked above does I've done this many times, it's an easy fix
TotechsStrypper
TotechsStrypperOP2y ago
Do you know how to do that with my current test ? I am a complete noob in FakeItEasy
Jayy
Jayy2y ago
Read the readme of the repo i just sent lol
TotechsStrypper
TotechsStrypperOP2y ago
TotechsStrypper
TotechsStrypperOP2y ago
wait
Jayy
Jayy2y ago
No lol, the library already handles all that for you Just read the docs and install the fake it easy version of the lib
TotechsStrypper
TotechsStrypperOP2y ago
oh
TotechsStrypper
TotechsStrypperOP2y ago
Jayy
Jayy2y ago
There ya go
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server