C
Join ServerC#
help
❔ Unit test: The source 'IQueryable' doesn't implement 'IAsyncEnumerable<T>'
TTotechsStrypper2/18/2023
My test keep failing when it trying to get to this line of code
Detail
FindAll
Controller
Test
var foods = await _foodRepository.FindAll().ToListAsync(cancellationToken);
Detail
FindAll
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));
}
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
}
Pphaseshift2/18/2023
Iqueryable is ienumerable, not the async version
TTotechsStrypper2/18/2023
I still don't understand these type can you provide me a fix for this situation ?
Pphaseshift2/18/2023
Don't use ToListAsync
Pphaseshift2/18/2023
ToList() should work
Pphaseshift2/18/2023
Although ToList generally not a good idea to trigger on db queries
TTotechsStrypper2/18/2023
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 ?
Pphaseshift2/18/2023
Maybe you just need to add
using System. Data.Entity
using System. Data.Entity
PPontiacGTX2/18/2023
what the repository implementation
PPontiacGTX2/18/2023
probably it fails due to the query?
TTotechsStrypper2/18/2023

TTotechsStrypper2/18/2023
the mock is not implement the right interface
PPontiacGTX2/18/2023
what is where if?
PPontiacGTX2/18/2023
there showing you to send the predicate and would do it where isn't null
PPontiacGTX2/18/2023
but you didnt send a predicate then it is null?
PPontiacGTX2/18/2023
_foodRepository.FindAll()
PPontiacGTX2/18/2023
so rather change the implementation for the FindAll repository's Method
JJayy2/18/2023
@TotechsStrypper use this https://github.com/romantitov/MockQueryable
JJayy2/18/2023
???
JJayy2/18/2023
Good lord y'all lol
JJayy2/18/2023
You have to mock async stuff a little diff
TTotechsStrypper2/18/2023
So it cannot face the IQueryable
a guy help me create a custom interface
var foods = A.Fake<IEFMock>();
a guy help me create a custom interface
var foods = A.Fake<IEFMock>();
public interface IEFMock : IQueryable<Food>, IAsyncEnumerable<Food>
{
}
JJayy2/18/2023
This will crash at runtime
JJayy2/18/2023
You have to mock all of IAsyncEnumerable
JJayy2/18/2023
Which the nuget i linked above does
JJayy2/18/2023
I've done this many times, it's an easy fix
TTotechsStrypper2/18/2023
Do you know how to do that with my current test ?
I am a complete noob in FakeItEasy
I am a complete noob in FakeItEasy
JJayy2/18/2023
Read the readme of the repo i just sent lol
TTotechsStrypper2/18/2023

TTotechsStrypper2/18/2023
wait
JJayy2/18/2023
No lol, the library already handles all that for you
JJayy2/18/2023
Just read the docs and install the fake it easy version of the lib
TTotechsStrypper2/18/2023
oh
TTotechsStrypper2/18/2023

JJayy2/18/2023
There ya go
AAccord2/19/2023
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.