public void ShouldThrowAnExceptionWhenPokemonFavoriteAlreadyExists() { #region Given PokemonId pokemonId = PokemonIdMother.PokemonId(); PokemonFavorite pokemonFavorite = new PokemonFavorite(pokemonId); string userId = UserIdMother.Id(); string expectedMessage = $"The pokemon with Id '{pokemonId.Id}' already exists in user favorites list"; User user = UserMother.UserWithFavorites(userId, pokemonId.Id); var userRepository = new Mock <UserRepository>(); userRepository .Setup(r => r.Find(It.IsAny <UserId>())) .ReturnsAsync(UserMother.UserWithFavorites(userId, pokemonId.Id)); userRepository .Setup(r => r.SaveFavorites(It.IsAny <User>())); PokemonFavoriteCreator pokemonFavoriteCreator = new PokemonFavoriteCreator(userRepository.Object); #endregion #region When var exception = Record.ExceptionAsync(async() => await pokemonFavoriteCreator.Execute(user, pokemonFavorite)); #endregion #region Then Assert.Equal(expectedMessage, exception.Result.Message); #endregion }
private void GuardPokemonFavoriteExistsInUser(PokemonFavorite favorite) { if (Favorites.Any(pokemonFavorite => pokemonFavorite.PokemonId.Id == favorite.PokemonId.Id)) { throw new PokemonFavoriteExistsException(favorite); } }
public async Task ShouldCreatePokemonFavorite() { #region Given PokemonId pokemonId = PokemonIdMother.PokemonId(); PokemonFavorite pokemonFavorite = new PokemonFavorite(pokemonId); string userId = UserIdMother.Id(); User user = new User(new UserId(userId)); var userRepository = new Mock <UserRepository>(); userRepository .Setup(r => r.SaveFavorites(It.IsAny <User>())); PokemonFavoriteCreator pokemonFavoriteCreator = new PokemonFavoriteCreator(userRepository.Object); #endregion #region When await pokemonFavoriteCreator.Execute(user, pokemonFavorite); #endregion #region Then userRepository.Verify(r => r.SaveFavorites(It.IsAny <User>()), Times.Once()); #endregion }
public void AddFavorite(PokemonFavorite favorite) { GuardPokemonFavoriteExistsInUser(favorite); Favorites.Add(favorite); }
public void AddPokemonFavorite(PokemonFavorite favorite) { PokemonFavorites.AddFavorite(favorite); }
public PokemonFavoriteExistsException(PokemonFavorite pokemonName) { _pokemonFavorite = pokemonName; }
public async Task Execute(User user, PokemonFavorite pokemonFavorite) { user.AddPokemonFavorite(pokemonFavorite); await _userRepository.SaveFavorites(user); }