public void AddPokemonToUserFavorites_ReturnsUserNotFoundException() { #region Arrange int pokemonId = PokemonIdMother.Id(); string userId = UserIdMother.Id(); string expectedMessage = $"User '{userId}' does not exists"; var userRepository = new Mock <UserRepository>(); userRepository .Setup(r => r.SaveFavorites(It.IsAny <User>())); UserFinder userFinder = new UserFinder(userRepository.Object); PokemonFavoriteCreator pokemonFavoriteCreator = new PokemonFavoriteCreator(userRepository.Object); AddPokemonToUserFavorites addPokemonToUserFavorites = new AddPokemonToUserFavorites(userFinder, pokemonFavoriteCreator); #endregion #region Act var exception = Record.ExceptionAsync(async() => await addPokemonToUserFavorites.Execute(userId, pokemonId)); #endregion #region Assert Assert.Equal(expectedMessage, exception.Result.Message); #endregion }
public async Task AddPokemonToUserFavorites_ReturnsVoid() { #region Arrange int pokemonId = PokemonIdMother.Id(); string userId = UserIdMother.Id(); var userRepository = new Mock <UserRepository>(); userRepository .Setup(r => r.Find(It.IsAny <UserId>())) .ReturnsAsync(UserMother.User(userId)); userRepository .Setup(r => r.Exists(It.IsAny <UserId>())) .ReturnsAsync(true); userRepository .Setup(r => r.SaveFavorites(It.IsAny <User>())); UserFinder userFinder = new UserFinder(userRepository.Object); PokemonFavoriteCreator pokemonFavoriteCreator = new PokemonFavoriteCreator(userRepository.Object); AddPokemonToUserFavorites addPokemonToUserFavorites = new AddPokemonToUserFavorites(userFinder, pokemonFavoriteCreator); #endregion #region Act await addPokemonToUserFavorites.Execute(userId, pokemonId); #endregion #region Assert userRepository.Verify(r => r.SaveFavorites(It.IsAny <User>()), Times.Once()); #endregion }
public async Task <IActionResult> Put([Required][FromHeader(Name = "userId")] string userId, int pokemonId) { try { await _addPokemonToUserFavorites.Execute(userId, pokemonId); return(Created($"user/addFavorite/{pokemonId}", pokemonId)); } catch (UserNotFoundException ex) { return(NotFound(ex.Message)); } catch (PokemonFavoriteExistsException ex) { return(Conflict(ex.Message)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public void AddPokemonToUserFavorites_ReturnsPokemonAlreadyExistsException() { #region Arrange int pokemonId = PokemonIdMother.Id(); string userId = UserIdMother.Id(); string expectedMessage = $"The pokemon with Id '{pokemonId}' already exists in user favorites list"; var userRepository = new Mock <UserRepository>(); userRepository .Setup(r => r.Find(It.IsAny <UserId>())) .ReturnsAsync(UserMother.UserWithFavorites(userId, pokemonId)); userRepository .Setup(r => r.Exists(It.IsAny <UserId>())) .ReturnsAsync(true); userRepository .Setup(r => r.SaveFavorites(It.IsAny <User>())); UserFinder userFinder = new UserFinder(userRepository.Object); PokemonFavoriteCreator pokemonFavoriteCreator = new PokemonFavoriteCreator(userRepository.Object); AddPokemonToUserFavorites addPokemonToUserFavorites = new AddPokemonToUserFavorites(userFinder, pokemonFavoriteCreator); #endregion #region Act var exception = Record.ExceptionAsync(async() => await addPokemonToUserFavorites.Execute(userId, pokemonId)); #endregion #region Assert Assert.Equal(expectedMessage, exception.Result.Message); #endregion }