Injection error of dependency

Asked

Viewed 164 times

1

inserir a descrição da imagem aqui

I’m using Xunit to carry out my tests, I am trying to access my CampeonatosController, consequently I have to pass the value shown in the image below as parameter, but I am getting the error below.

You would know how to help me.

My test class:

public class CampeonatoTest
{
    private const string IdReturnsOk = "2021";
    private const string IdNotFound = "XXXX";

    private readonly CampeonatosController _campeonato;

    private readonly ICompetitionService _competitionManager;

    public CampeonatoTest(
        CompetitionService competitionManager)
    {
        _competitionManager = competitionManager;

        _campeonato = new CampeonatosController(_competitionManager);
    }

    [Fact]
    public async Task Campeonato_GetById_ValuesReturnsOkResponse()
    {
        var response = await _campeonato.Get(IdReturnsOk);

        var objectResponse = response as ObjectResult;

        Assert.Equal(200, objectResponse.StatusCode);
    }

    [Fact]
    public async Task Campeonato_GetById_ReturnsNotFoundResponse()
    {
        var response = await _campeonato.Get(IdNotFound);

        var objectResponse = response as ObjectResult;

        Assert.Equal(404, objectResponse.StatusCode);
    }
}

And here’s my Controller:

public class CampeonatosController : ControllerBase
{
    private readonly ICompetitionService _competitionManager;

    public CampeonatosController(
        CompetitionService competitionManager) 
    {
        _competitionManager = competitionManager;
    }

    // GET api/campeonatos/5
    [HttpGet("{id}")]
    public async Task<IActionResult> Get(string id)
    {
        HttpResponseMessage respToken = await _competitionManager.GetIdCompetitionAsync(id);

        string conteudo = respToken.Content.ReadAsStringAsync().Result;

        if (respToken.StatusCode == HttpStatusCode.OK)
        {
            return Ok(conteudo);
        }
        else
        {
            return StatusCode(StatusCodes.Status500InternalServerError);
        }
    }

}

1 answer

2


Note that the controller asks for the specific type, not the interface. This is wrong.

public CampeonatosController(CompetitionService competitionManager) 
{
    _competitionManager = competitionManager;
}

Should be

public CampeonatosController(ICompetitionService competitionManager) 
{
    _competitionManager = competitionManager;
}
  • worse than this implemented correctly, was reading in some places what should do with NMock, but I couldn’t implement it correctly.

  • @Matheus is wrong in the controller, right. I’ll edit the answer.

  • I made this change but I keep getting the error Message: The following constructor parameters did not have matching fixture data: ICompetitionService competitionManager

  • @Matheus This mistake has nothing to do with the question.

  • apparently it is not possible to use dependency injection in test applications.

  • It is possible yes, you must be failing implementation or something. Anyway, his original question was about a problem and this problem is totally unrelated. If you need help solving the new problem, open a new question. Otherwise, we will end up completely escaping the scope of this publication here.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.