0

I'm facing a problem while testing my controller. Testing is done with an MStestProject.

Actually I want as a first test make a basic operation: just having a method that returns null, so my controller will send a NotFound as result

Here is my controller

[ApiController]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class ProfessionalController : MyControllerBase
{
    private readonly ProfessionalMapper _mapper;
    private readonly IProfessionalRepository _professionalRepository;
    private readonly IIndividualRepository _individualRepository;
    private readonly IReferenceListRepository _referenceListRepository;

    public ProfessionalController(
        ProfessionalMapper mapper,
        IProfessionalRepository professionalRepository,
        IIndividualRepository individualRepository,
        IReferenceListRepository referenceListRepository)
    {
        _mapper = mapper;
        _professionalRepository = professionalRepository;
        _individualRepository = individualRepository;
        _referenceListRepository = referenceListRepository;
    }

    

    [ProducesResponseType(typeof(Professional), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [HttpGet("{professionalId}")]
    public async Task<ActionResult<Professional>> GetProfessionalAsync(Guid professionalId, [FromQuery] string? includes)
    {
        var result = await _professionalRepository.GetProfessionals()
            .ById(hcProfessionalId)
            .ApplyIncludes(includes)
            .FirstOrDefaultAsync();

        return result is null ? NotFound() : Ok(_mapper.Map(result, includes));
    }}

Here is the test method

    [TestInitialize]
    public void Initialize()
    {
        _professionalRepositoryMock = new Mock<IProfessionalRepository>();
        var localizerMock = new Mock<Localizer>(new Mock<IHttpContextAccessor>().Object);

        // Create a mock for the IndividualMapper class
        var individualMapperMock = new Mock<IndividualMapper>();

        // Create an instance of ProfessionalMapper using the mocked dependencies
        _hcProfessionalMapper = new ProfessionalMapper(localizerMock.Object, () => individualMapperMock.Object);

        _controller = new ProfessionalController(
            _professionalMapper,
            _professionalRepositoryMock.Object,
            Mock.Of<IIndividualRepository>(),
            Mock.Of<IReferenceListRepository>());
    }

    [TestMethod]
    public async Task GetProfessionalAsync_NonExistingId_ReturnsNotFoundResult()
    {
        // Arrange
        var professionalId = Guid.NewGuid();
        _professionalRepositoryMock.Setup(r => r.GetProfessionals()).Returns(Enumerable.Empty<Professional>().AsQueryable());
        // Act
        var result = await _controller.GetProfessionalAsync(professionalId , null);

        // Assert
        Assert.IsNotNull(result);
        Assert.IsInstanceOfType(result.Result, typeof(NotFoundResult));
    }

If I make a break point, I'm able to run to the .. var result = await... line but when I hit the The F10 button I've got this error message:

System.InvalidOperationException: The provider for the source 'IQueryable' doesn't implement 'IAsyncQueryProvider'. Only providers that implement 'IAsyncQueryProvider' can be used for Entity Framework asynchronous operations.

it seems the system complains due to the FristOrDefaultAsync... but I don't have any clue to overcome this challenge

Does anyone have any idea?

Regards;

Refre
  • 65
  • 2
  • 11
  • Does this answer your question? [How to mock an async repository with Entity Framework Core](https://stackoverflow.com/questions/40476233/how-to-mock-an-async-repository-with-entity-framework-core) – YK1 Jun 25 '23 at 19:38
  • The _professionalRepositoryMock setting returns a synchronous IEnumerable instead of an asynchronous IQueryable when .GetProfessionals() is called. – wenbingeng-MSFT Aug 16 '23 at 08:09

0 Answers0