public void GetCurrency_WhenCalledAndReturnsOtherThan200And404_ShouldThrowException() { //Arrange var mockHttpMessageHandler = new Mock <HttpMessageHandler>(); mockHttpMessageHandler .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.BadRequest }); var nbpService = new NbpService(new HttpClient(mockHttpMessageHandler.Object)); //Act //Assert Assert.ThrowsAsync <NbpApiException>(() => nbpService.GetCurrency("USD", new DateTime(2020, 1, 1))); }
public async Task GetCurrency_WhenCalledAndReturns404_ShouldReturnNull() { //Arrange var mockHttpMessageHandler = new Mock <HttpMessageHandler>(); mockHttpMessageHandler .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.NotFound }); var nbpService = new NbpService(new HttpClient(mockHttpMessageHandler.Object)); //Act var result = await nbpService.GetCurrency("USD", new DateTime(2020, 1, 1)); //Assert Assert.IsNull(result); }
public async Task GetCurrency_WhenCalledAndReturns200_ShouldReturnExpectedValue() { //Arrange var mockHttpMessageHandler = new Mock <HttpMessageHandler>(); mockHttpMessageHandler .Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(_mockedJson) }); var nbpService = new NbpService(new HttpClient(mockHttpMessageHandler.Object)); var expectedResult = new NbpResponse { Code = "USD", Currency = "dolar amerykañski", Rates = new List <NbpCurrencyRate>() { new NbpCurrencyRate { EffectiveDate = new DateTime(2020, 1, 2), Mid = 3.8000M } } }; //Act var result = await nbpService.GetCurrency("USD", new DateTime(2020, 1, 2)); //Assert Assert.AreEqual(result, expectedResult); }