Beispiel #1
0
        public void PokemonFinder_ReturnsException()
        {
            #region Arrange
            PokemonId pokemonId         = new PokemonId(PokemonIdMother.Id());
            var       pokemonRepository = new Mock <PokemonRepository>();
            string    expectedMessage   = $"Pokemon with Id '{pokemonId.Id}' does not exist";

            pokemonRepository
            .Setup(r => r.Find(It.IsAny <PokemonId>()))
            .ReturnsAsync(PokemonMother.Pokemon());

            var pokemonFinder = new PokemonFinder(pokemonRepository.Object);

            #endregion

            #region Act
            var exception = Record.ExceptionAsync(async() => await pokemonFinder.Execute(pokemonId));

            #endregion

            #region Assert
            Assert.Equal(expectedMessage, exception.Result.Message);

            #endregion
        }
Beispiel #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
        }
Beispiel #3
0
        public async Task PokemonFinder_ReturnsPokemon()
        {
            #region Arrange
            PokemonId pokemonId         = new PokemonId(PokemonIdMother.Id());
            var       pokemonRepository = new Mock <PokemonRepository>();

            pokemonRepository
            .Setup(r => r.Find(It.IsAny <PokemonId>()))
            .ReturnsAsync(PokemonMother.Pokemon());

            pokemonRepository
            .Setup(r => r.Exists(It.IsAny <PokemonId>()))
            .ReturnsAsync(true);

            var pokemonFinder = new PokemonFinder(pokemonRepository.Object);

            #endregion

            #region Act
            Pokemon pokemon = await pokemonFinder.Execute(pokemonId);

            #endregion

            #region Assert
            Assert.Equal(pokemon.PokemonId.Id, PokemonMother.Pokemon().PokemonId.Id);
            Assert.Equal(pokemon.PokemonName.Name, PokemonMother.Pokemon().PokemonName.Name);
            Assert.Equal(pokemon.PokemonTypes.Types.Select(s => s.Type).ToArray(), PokemonMother.Pokemon().PokemonTypes.Types.Select(s => s.Type).ToArray());

            #endregion
        }
Beispiel #4
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
        }
Beispiel #5
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
        }
        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 GetPokemonFavorites_ReturnsPokemonFavorites()
        {
            #region Arrange
            string           id               = UserIdMother.Id();
            UserId           userId           = UserIdMother.UserId();
            int              pokemonId        = PokemonIdMother.Id();
            PokemonFavorites pokemonFavorites = PokemonFavoritesMother.PokemonFavorites();

            var userRepository = new Mock <UserRepository>();

            userRepository
            .Setup(r => r.Find(It.IsAny <UserId>()))
            .ReturnsAsync(UserMother.UserWithFavorites(id, pokemonId));

            userRepository
            .Setup(r => r.Exists(It.IsAny <UserId>()))
            .ReturnsAsync(true);

            UserFinder userFinder = new UserFinder(userRepository.Object);
            PokemonFavoriteSearcher pokemonFavoriteSearcher = new PokemonFavoriteSearcher();
            GetPokemonUserFavorites getPokemonUserFavorites = new GetPokemonUserFavorites(userFinder, pokemonFavoriteSearcher);

            #endregion

            #region Act
            PokemonFavorites favorites = await getPokemonUserFavorites.Execute(id);

            #endregion

            #region Assert
            Assert.True(pokemonFavorites.Favorites.All(f => favorites.Favorites.Any(item =>
                                                                                    item.PokemonId.Id == f.PokemonId.Id)));

            #endregion
        }
 public static Pokemon Pokemon()
 {
     return(new Pokemon(new PokemonId(PokemonIdMother.Id()),
                        new PokemonName(PokemonNameMother.Name()),
                        new PokemonTypes(new List <PokemonType>()
     {
         new PokemonType("fire"),
         new PokemonType("flying"),
     })));
 }
Beispiel #10
0
        public async Task Search_NotFound_ReturnsNull()
        {
            #region Arrange

            PokeApiPokemonRepository pokemonRepository = new PokeApiPokemonRepository();
            PokemonId pokemonId = new PokemonId(PokemonIdMother.IdUnknown());

            #endregion

            #region Act
            Pokemon pokemon = await pokemonRepository.Find(pokemonId);

            #endregion

            #region Assert
            Assert.Null(pokemon);

            #endregion
        }
Beispiel #11
0
        public async Task ExistsUser_ReturnsBool()
        {
            #region Arrange

            PokeApiPokemonRepository pokemonRepository = new PokeApiPokemonRepository();
            PokemonId pokemonId = new PokemonId(PokemonIdMother.Id());

            #endregion

            #region Act
            bool exists = await pokemonRepository.Exists(pokemonId);

            #endregion

            #region Assert
            Assert.True(exists);

            #endregion
        }
Beispiel #12
0
        public async Task Search_Found_ReturnsPokemonDetail()
        {
            #region Arrange

            PokeApiPokemonRepository pokemonRepository = new PokeApiPokemonRepository();
            PokemonId pokemonId = new PokemonId(PokemonIdMother.Id());

            #endregion

            #region Act
            Pokemon pokemon = await pokemonRepository.Find(pokemonId);

            #endregion

            #region Assert
            var typesArray = pokemon.PokemonTypes.Types.Select(s => s.Type).ToArray();

            Assert.Equal(pokemon.PokemonId.Id, PokemonMother.Pokemon().PokemonId.Id);
            Assert.Equal(pokemon.PokemonName.Name, PokemonMother.Pokemon().PokemonName.Name);
            Assert.Equal(typesArray, PokemonMother.Pokemon().PokemonTypes.Types.Select(s => s.Type).ToArray(), StringComparer.InvariantCultureIgnoreCase);

            #endregion
        }
        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
        }