Test unitaire : POST + GET

pull/1/head
nathan boileau 2 years ago
parent 2f6c475ecd
commit ec9ad445da

@ -7,30 +7,49 @@ namespace TestUnitaire
{ {
public class TestAPILol public class TestAPILol
{ {
[Fact] [Theory]
public void Test1() [InlineData("Aatrox", "Aatrox est un champion de League of Legends")]
[InlineData("Ahri", "Ahri est un champion de League of Legends")]
[InlineData("Akali", "Akali est un champion de League of Legends")]
public async Task TestPostChampion(string name, string bio)
{ {
// Arrange
var data = new StubData();
var controller = new ControllerChampions(new StubData());
var champDTO = new ChampionDTO(name, bio);
// Act
var nbInListBefore = data.ChampionsMgr.GetNbItems().Result;
var result = await controller.Post(champDTO);
var nbInListAfter = data.ChampionsMgr.GetNbItems().Result;
// Assert
// IS the champion added to the list, number of champions in the list + 1
Assert.Equal(nbInListBefore + 1, nbInListAfter);
} }
[Fact]
public void TestPostChampion() [Theory]
[InlineData("Aatrox", "Aatrox est un champion de League of Legends")]
[InlineData("Ahri", "Ahri est un champion de League of Legends")]
[InlineData("Akali", "Akali est un champion de League of Legends")]
public async Task TestGetChampion(string name, string bio)
{ {
// Arrange // Arrange
var data = new StubData(); var data = new StubData();
var controller = new ControllerChampions(new StubData()); var controller = new ControllerChampions(new StubData());
var champDTO = new ChampionDTO("Charles", "Charles est un champion de League of Legends"); var champDTO = new ChampionDTO(name, bio);
// Act // Act
var result = controller.Post(champDTO); // Call method POST to add a champion
data.ChampionsMgr.AddItem(champDTO.ToModel()); var result = await controller.Post(champDTO);
var nbItem = data.ChampionsMgr.GetNbItems(); // Call method GET to get the champion
Task<int> nbItemTask = nbItem; var resultGet = await controller.GetChampion(name);
// Assert // Assert
// Verify that the champions is added to the stub // IS the champion added to the list, number of champions in the list + 1
Assert.Equal(7, nbItemTask.Result); Assert.Equal(name, champDTO.Name);
Assert.Equal(bio, champDTO.Bio);
} }
} }
} }

@ -42,12 +42,12 @@ namespace apiLOL.Controllers
// POST api/<ControllerLol> // POST api/<ControllerLol>
[HttpPost] [HttpPost]
public async Task<IActionResult> Post([FromBody] ChampionDTO champDTO) public async Task<IActionResult> Post(ChampionDTO champDTO)
{ {
Champion tmp = champDTO.ToModel(); Champion tmp = champDTO.ToModel();
Champion champ = await data.ChampionsMgr.AddItem(tmp); Champion champion = await data.ChampionsMgr.AddItem(tmp);
ChampionDTO dto = champ.ToDTO(); ChampionDTO dtoChamp = champion.ToDTO();
return CreatedAtAction(nameof(GetChampion), new { name = dto.Name }, dto); return CreatedAtAction(nameof(GetChampion), new { name = dtoChamp.Name }, dtoChamp);
} }
// PUT api/<ControllerLol>/5 // PUT api/<ControllerLol>/5

Loading…
Cancel
Save