Example #1
0
        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
        }
Example #2
0
        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
        }
Example #3
0
        public async Task SaveFavorites_ReturnsUserWithFavorites()
        {
            #region Arrange

            InMemoryUserRepository inMemoryUserRepository = new InMemoryUserRepository(memoryCache);
            UserId    userId    = new UserId(UserIdMother.Id());
            User      user      = new User(userId);
            PokemonId pokemonId = PokemonIdMother.PokemonId();
            user.AddPokemonFavorite(new PokemonFavorite(pokemonId));

            #endregion

            #region Act
            await inMemoryUserRepository.Save(user);

            User userFound = await inMemoryUserRepository.Find(userId);

            await inMemoryUserRepository.SaveFavorites(user);

            #endregion

            #region Assert
            var pokemonFavoritesArray = userFound.PokemonFavorites.Favorites.Select(s => s.PokemonId.Id.ToString()).ToArray();
            Assert.Equal(pokemonFavoritesArray, PokemonFavoritesMother.PokemonFavorites().Favorites.Select(s => s.PokemonId.Id.ToString()).ToArray(), StringComparer.InvariantCultureIgnoreCase);

            #endregion
        }