示例#1
0
        public async Task ShouldReturnPokemonTranslatedDescription()
        {
            var baseUriPoke         = new Uri("https://pokeapi.co/api/v2/pokemon-species/");
            var baseUriTranslation  = new Uri("https://api.funtranslations.com/translate/shakespeare.json/");
            var pokemonName         = "ditto";
            var expectedDescription = "'t can freely recombine its own cellular structure to transform into other life-forms.";
            var log             = new Mock <ILogger <string> >();
            var jsonPoke        = ReadEmbeddedResource($"{pokemonName}-pokemon-species.json");
            var jsonTranslation = ReadEmbeddedResource($"{pokemonName}-translation.json");
            var mockHttp        = new MockHttpMessageHandler();

            mockHttp.When($"{baseUriPoke}{pokemonName}/").Respond("application/json", jsonPoke);
            mockHttp.When($"{baseUriTranslation}*").Respond("application/json", jsonTranslation);
            var pokeAPIClient    = new PokeAPIClient(mockHttp.ToHttpClient());
            var translatorClient = new TranslatorClient(mockHttp.ToHttpClient());
            var pokemonRetriever = new PokemonRetriever(pokeAPIClient, translatorClient);
            var controller       = new PokemonController(pokemonRetriever, log.Object);

            var response = await controller.GetAsync(pokemonName);

            var result        = (ObjectResult)response.Result;
            var pokemonResult = (Pokemon)result.Value;

            Assert.Equal(pokemonName, pokemonResult.Name);
            Assert.Equal(expectedDescription, pokemonResult.Description);
        }
示例#2
0
        public async Task ShouldReturnNotFound()
        {
            var pokemonName      = "ditto";
            var log              = new Mock <ILogger <string> >();
            var mockHttp         = new MockHttpMessageHandler();
            var pokeAPIClient    = new PokeAPIClient(mockHttp.ToHttpClient());
            var translatorClient = new TranslatorClient(mockHttp.ToHttpClient());
            var pokemonRetriever = new PokemonRetriever(pokeAPIClient, translatorClient);
            var controller       = new PokemonController(pokemonRetriever, log.Object);

            var response = await controller.GetAsync(pokemonName);

            var result = (ObjectResult)response.Result;

            Assert.Equal(StatusCodes.Status404NotFound, result.StatusCode.Value);
        }
示例#3
0
        public async Task ShouldReturnInternalError()
        {
            var pokemonName      = "ditto";
            var log              = new Mock <ILogger <string> >();
            var mockHttp         = new MockHttpMessageHandler();
            var pokemonRetriever = new Mock <IPokemonRetriever>();

            pokemonRetriever.Setup(x => x.GetDescriptionAsync(It.IsAny <string>())).Throws(new Exception());
            var controller = new PokemonController(pokemonRetriever.Object, log.Object);

            var response = await controller.GetAsync(pokemonName);

            var result = (StatusCodeResult)response.Result;

            Assert.Equal(StatusCodes.Status500InternalServerError, result.StatusCode);
        }
示例#4
0
        public async Task ShouldReturnNotFound()
        {
            var pokemonName      = "ditto";
            var log              = new Mock <ILogger <string> >();
            var mockHttp         = new MockHttpMessageHandler();
            var pokemonRetriever = new Mock <IPokemonRetriever>();

            pokemonRetriever.Setup(x => x.GetDescriptionAsync(It.IsAny <string>())).ReturnsAsync(Result <string> .Error("Error"));
            var controller = new PokemonController(pokemonRetriever.Object, log.Object);

            var response = await controller.GetAsync(pokemonName);

            var result = (ObjectResult)response.Result;

            Assert.Equal(StatusCodes.Status404NotFound, result.StatusCode.Value);
        }
示例#5
0
        public async Task ShouldReturnPokemonTranslatedDescription()
        {
            var pokemonName         = "ditto";
            var expectedDescription = "'t can freely recombine its own cellular structure to transform into other life-forms.";
            var log = new Mock <ILogger <string> >();
            var pokemonRetriever = new Mock <IPokemonRetriever>();

            pokemonRetriever.Setup(x => x.GetDescriptionAsync(It.IsAny <string>())).ReturnsAsync(Result <string> .Success(expectedDescription));
            var controller = new PokemonController(pokemonRetriever.Object, log.Object);

            var response = await controller.GetAsync(pokemonName);

            var result        = (ObjectResult)response.Result;
            var pokemonResult = (Pokemon)result.Value;

            Assert.Equal(pokemonName, pokemonResult.Name);
            Assert.Equal(expectedDescription, pokemonResult.Description);
        }
示例#6
0
        public void TestEmptyPokemonNameReturnsApplicationException()
        {
            ApplicationException ex = Assert.ThrowsAsync <ApplicationException>(async() => await pokemonController.GetAsync(string.Empty));

            Assert.That(ex.Message, Is.EqualTo("Pokemon name must be passed in the URI as a parameter."));
        }