Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created March 16, 2023 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/1ae12069476fb5d39a60661a9e7d8363 to your computer and use it in GitHub Desktop.
Save dcomartin/1ae12069476fb5d39a60661a9e7d8363 to your computer and use it in GitHub Desktop.
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