public async Task CreationShouldReturnEntityInsertedWithId() { var idAssigned = 1; var name = "hahaha"; var houseId = "hehehe"; var school = "hihihi"; var role = "hohoho"; var patronus = "huhuhu"; var characterDTO = new CharacterDTO { Name = name, House = houseId, School = school, Role = role, Patronus = patronus }; var houseModel = new HouseModel { Id = houseId }; var characterEntityMock = Mock.Of <Character>(c => c.Id == idAssigned && c.Name == name && c.House == houseId && c.School == school && c.Role == role && c.Patronus == patronus); var apiResult = Result <HouseModel> .Success(houseModel); var makeMagicApiClientMock = Mock.Of <MakeMagicApiClient>(mmac => mmac.GetHouse(houseId) == Task.FromResult(apiResult)); var characterRepository = Mock.Of <CharactersRepository>(cr => cr.Insert(It.Is <Character>(c => c.Name == name && c.House == houseId && c.School == school && c.Role == role && c.Patronus == patronus)) == Task.FromResult(idAssigned) && cr.Get(idAssigned) == Task.FromResult(characterEntityMock)); var characterEditor = new CharacterEditor(makeMagicApiClientMock, characterRepository); var result = await characterEditor.Create(characterDTO); Assert.False(result.Error); Assert.Equal(characterEntityMock, result.Value); }
public async Task <IActionResult> Post(CharacterDTO newCharacter) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var characterCreationResult = await _characterEditor.Create(newCharacter); return(HandleResponseDefault(characterCreationResult, () => CreatedAtAction(nameof(Get), new { id = characterCreationResult.Value.Id }, characterCreationResult.Value))); }
public async Task CreationShouldPropagateApiCallErrorIfThereWasAny() { var houseId = "hehehe"; var characterDTO = Mock.Of <CharacterDTO>(c => c.House == houseId); var exceptedApiResult = Result <HouseModel> .Failed(ErrorLevel.UnrecoverableError, "Erro Api"); var makeMagicApiClientMock = Mock.Of <MakeMagicApiClient>(mmac => mmac.GetHouse(houseId) == Task.FromResult(exceptedApiResult)); var characterEditor = new CharacterEditor(makeMagicApiClientMock, Mock.Of <CharactersRepository>()); var result = await characterEditor.Create(characterDTO); Assert.True(result.Error); Assert.Equal(exceptedApiResult.ErrorLevel, result.ErrorLevel); Assert.Contains(exceptedApiResult.ErrorMessage, result.ErrorMessage); }
public async Task CreationShouldFailWithRecoverableErrorIfHouseIsInvalid() { var houseId = "hehehe"; var characterDTO = Mock.Of <CharacterDTO>(c => c.House == houseId); var apiResult = Result <HouseModel> .Success(null); var makeMagicApiClientMock = Mock.Of <MakeMagicApiClient>(mmac => mmac.GetHouse(houseId) == Task.FromResult(apiResult)); var characterEditor = new CharacterEditor(makeMagicApiClientMock, Mock.Of <CharactersRepository>()); var result = await characterEditor.Create(characterDTO); Assert.True(result.Error); Assert.Equal(ErrorLevel.RecoverableError, result.ErrorLevel); Assert.Contains("O id da casa da informado não corresponde a nenhuma casa conhecida.", result.ErrorMessage); }