This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExchangeRateClientTests | |
{ | |
private readonly MemoryCache _cache; | |
public ExchangeRateClientTests() | |
{ | |
_cache = new MemoryCache(new MemoryCacheOptions()); | |
} | |
[Fact] | |
public async Task Rate() | |
{ | |
var exchangeRate = 1.5m; | |
var httpClient = new HttpClient(new TestHttpHandler(Currency.CAD, exchangeRate)); | |
var obj = new ExchangeRateClient(httpClient, _cache); | |
var result = await obj.Rate(new DateOnly(2022, 12, 01), Currency.USD, Currency.CAD); | |
Assert.Equal(exchangeRate, result); | |
} | |
} | |
public class TestHttpHandler : HttpMessageHandler | |
{ | |
private readonly Currency _toCurrency; | |
private readonly decimal _exchangeRate; | |
public TestHttpHandler(Currency toCurrency, decimal exchangeRate) | |
{ | |
_toCurrency = toCurrency; | |
_exchangeRate = exchangeRate; | |
} | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | |
{ | |
var response = new HttpResponseMessage(HttpStatusCode.OK); | |
var json = JsonConvert.SerializeObject(new { Rates = new Dictionary<string, decimal> { { _toCurrency.ToString(), _exchangeRate } } }); | |
response.Content = new StringContent(json); | |
return Task.FromResult(response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment