public async Task UpdateShouldAlterPropertiesOfEntity()
        {
            var name                = "hahaha";
            var houseId             = "hehehe";
            var school              = "hihihi";
            var role                = "hohoho";
            var patronus            = "huhuhu";
            var newCharacterInfoDTO = new CharacterDTO
            {
                Name     = name,
                House    = houseId,
                School   = school,
                Role     = role,
                Patronus = patronus
            };
            var houseModel = new HouseModel
            {
                Id = houseId
            };
            var characterEntityMock = new Mock <Character>();

            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.Update(characterEntityMock.Object) == Task.FromResult(true));

            var characterEditor = new CharacterEditor(makeMagicApiClientMock, characterRepository);
            var result          = await characterEditor.Update(characterEntityMock.Object, newCharacterInfoDTO);

            Assert.False(result.Error);
            Assert.Equal(characterEntityMock.Object, result.Value);
            characterEntityMock.Verify(c => c.Update(newCharacterInfoDTO.Name, newCharacterInfoDTO.Role, newCharacterInfoDTO.School, newCharacterInfoDTO.House, newCharacterInfoDTO.Patronus),
                                       Times.Once());
        }
예제 #2
0
        public async Task <IActionResult> Put(int id, CharacterDTO character)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var characterFromDb = await _characterRepository.Get(id);

            if (characterFromDb is null)
            {
                return(NotFound());
            }
            var characterUpdateResult = await _characterEditor.Update(characterFromDb, character);

            return(HandleResponseDefault(characterUpdateResult, () => NoContent()));
        }
        public async Task UpdateShouldPropagateApiCallErrorIfThereWasAny()
        {
            var houseId         = "hehehe";
            var characterDTO    = Mock.Of <CharacterDTO>(c => c.House == houseId);
            var characterFromDb = Mock.Of <Character>(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.Update(characterFromDb, characterDTO);

            Assert.True(result.Error);
            Assert.Equal(exceptedApiResult.ErrorLevel, result.ErrorLevel);
            Assert.Contains(exceptedApiResult.ErrorMessage, result.ErrorMessage);
        }
        public async Task UpdateShouldFailWithRecoverableErrorIfHouseIsInvalid()
        {
            var houseId      = "hehehe";
            var characterDTO = Mock.Of <CharacterDTO>(c => c.House == houseId);

            var characterFromDb = Mock.Of <Character>(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.Update(characterFromDb, 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);
        }