1
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);
}
}
}
worse than this implemented correctly, was reading in some places what should do with
NMock
, but I couldn’t implement it correctly.– Matheus
@Matheus is wrong in the controller, right. I’ll edit the answer.
– Jéf Bueno
I made this change but I keep getting the error
Message: The following constructor parameters did not have matching fixture data: ICompetitionService competitionManager
– Matheus
@Matheus This mistake has nothing to do with the question.
– Jéf Bueno
apparently it is not possible to use dependency injection in test applications.
– Matheus
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.
– Jéf Bueno