public async void ShouldReturnTooManyStatusCode() { //arrange var handlerMockPokeAPI = new Mock <HttpMessageHandler>(); var responsePokeAPI = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(@"{""flavor_text_entries"":[{""flavor_text"":""Its nature is to store up electricity. Forests\nwhere nests of Pikachu live are dangerous,\nsince the trees are so often struck by lightning."",""language"":{""name"":""en"",""url"":""https://pokeapi.co/api/v2/language/9/""},""version"":{""name"":""ultra-sun"",""url"":""https://pokeapi.co/api/v2/version/29/""}}]}"), }; handlerMockPokeAPI .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(responsePokeAPI); var httpClientPokeAPI = new HttpClient(handlerMockPokeAPI.Object); var handlerMockFunTranslationAPI = new Mock <HttpMessageHandler>(); var responseFunTranslationAPI = new HttpResponseMessage { StatusCode = HttpStatusCode.TooManyRequests, }; handlerMockFunTranslationAPI .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(responseFunTranslationAPI); var httpClientFunTranslationAPI = new HttpClient(handlerMockFunTranslationAPI.Object); var pokeApi = new PokeAPIRepository(httpClientPokeAPI); var funTranslationAPI = new FunTranslationAPIRepository(httpClientFunTranslationAPI); //act string nameOfPokemonToTranslate = "pikachu"; var pokemonService = new PokemonService(pokeApi, funTranslationAPI); var pokemon = await pokemonService.getPokemonFlavorTextTranslationAsync(nameOfPokemonToTranslate, "en"); //assert Assert.NotNull(pokemon); handlerMockPokeAPI.Protected().Verify( "SendAsync", Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get), ItExpr.IsAny <CancellationToken>()); handlerMockFunTranslationAPI.Protected().Verify( "SendAsync", Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Post), ItExpr.IsAny <CancellationToken>()); Assert.Equal(HttpStatusCode.TooManyRequests.GetHashCode().ToString(), pokemon.description); }
public void Initialize() { _config = Options.Create(new PokemonAPIConfig() { EndPoint = "http://localhost/pokemon/{0}" }); _httpClientWrapperMock = new Mock <IHttpClientWrapper>(); _pokeAPIRepository = new PokeAPIRepository(_config, _httpClientWrapperMock.Object); }
public async void ShouldReturnEmptyDescription() { //arrange var handlerMockPokeAPI = new Mock <HttpMessageHandler>(); var responsePokeAPI = new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound, }; handlerMockPokeAPI .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(responsePokeAPI); var httpClientPokeAPI = new HttpClient(handlerMockPokeAPI.Object); var handlerMockFunTranslationAPI = new Mock <HttpMessageHandler>(); var responseFunTranslationAPI = new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound, }; handlerMockFunTranslationAPI .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(responseFunTranslationAPI); var httpClientFunTranslationAPI = new HttpClient(handlerMockFunTranslationAPI.Object); var pokeApi = new PokeAPIRepository(httpClientPokeAPI); var funTranslationAPI = new FunTranslationAPIRepository(httpClientFunTranslationAPI); //act string nameOfPokemonToTranslate = "pikachu"; var pokemonService = new PokemonService(pokeApi, funTranslationAPI); var pokemon = await pokemonService.getPokemonFlavorTextTranslationAsync(nameOfPokemonToTranslate, "en"); //assert Assert.NotNull(pokemon); handlerMockPokeAPI.Protected().Verify( "SendAsync", Times.Exactly(1), ItExpr.Is <HttpRequestMessage>(req => req.Method == HttpMethod.Get), ItExpr.IsAny <CancellationToken>()); Assert.Equal(nameOfPokemonToTranslate, pokemon.name); Assert.Equal("", pokemon.description); }