Error trying to use Xunit

Asked

Viewed 63 times

0

I’m trying to use XUnit to test my application, but I am getting this error at the time of injeção de dependência. I get the following error:

Message: The following constructor Parameters Did not have matching fixture data: Icompetitionservice competitionManager

Class of Teste:

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

    private readonly CampeonatosController _campeonato;

    private readonly ICompetitionService _competitionManager;

    public CampeonatoTest(
        ICompetitionService 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 this is mine Controller:

private readonly ICompetitionService _competitionManager;

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

What am I doing wrong ?

1 answer

0

Dude, analyzing your code the problem is in your test class trying to receive the instance of a Icompetitionservice.

I believe you are mixing some concepts, because when we talk about unit testing, you should isolate the unit to be tested from its dependencies. As it stands, you are expecting a real implementation of Icompetitionservice.

This brings some problems to your test, for example, if Icompetitionservice accesses the database or an external api, or some Icompetitionservice dependency does these types of accesses, you have to ensure that the database and the api will be standing and available for your test to pass and that they have the exact behavior that your test expects.

One way to solve this problem is for you to simulate the behavior of your dependencies through Mocks or Stubs, instead of calling your actual dependency you will pass an object that will simulate the behavior of your dependency, perhaps by returning the given(s) your test waiting or casting some exception.

To understand the diff of Mock and Stubs, see another thread: What’s the difference between mock & stub?

See also this lib: https://github.com/moq/moq4

Browser other questions tagged

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