public static void ThrowIfError(this CLResponse response) { if (!response.Success) { throw CLException.FromCode(response.Error.Code, response.Error.Info); } }
public HttpClient CreateMockHttp(CLResponse currencyResponse) { var json = JsonConvert.SerializeObject(currencyResponse); string url = serviceBaseUrl; HttpResponseMessage httpResponse = new HttpResponseMessage(); httpResponse.StatusCode = System.Net.HttpStatusCode.OK; httpResponse.Content = new StringContent(json, Encoding.UTF8, "application/json"); Mock <HttpMessageHandler> mockHandler = new Mock <HttpMessageHandler>(); mockHandler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.Is <HttpRequestMessage>(r => r.Method == HttpMethod.Get && r.RequestUri.ToString().StartsWith(url)), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(httpResponse); HttpClient httpClient = new HttpClient(mockHandler.Object) { BaseAddress = new Uri(serviceBaseUrl) }; return(httpClient); }
public async Task TestCurrencyConversionUnavailable() { decimal rateEurUSD = 4; decimal ammount = 10; var currencyResponse = new CLResponse() { quotes = new Dictionary <string, decimal>() { { "EURUSD", rateEurUSD } } }; var mockHttp = CreateMockHttp(currencyResponse); service = new CurrencyService(mockHttp, serviceApiKeyMock); Func <Task> act = async() => { await service.Convert(ammount, "USD", "Z5"); }; act.Should().Throw <Exception>(); }
public async Task TestCurrencyConversionBackwards() { decimal rateEurUSD = 4; decimal ammount = 10; var currencyResponse = new CLResponse() { quotes = new Dictionary <string, decimal>() { { "EURUSD", 4 } } }; var mockHttp = CreateMockHttp(currencyResponse); service = new CurrencyService(mockHttp, serviceApiKeyMock); var eurUsdResult = await service.Convert(10, "USD", "EUR"); eurUsdResult.Should().Be(ammount / rateEurUSD); }
protected void AssertSuccess(CLResponse response) { Assert.IsNotNull(response); Assert.IsTrue(response.Success); }