I have the following code and I want to unit test it, as below
public class MarketLiability : IMarketLiability
{
private readonly string serviceAddress;
private readonly ILogger<MarketLiability> logger;
private readonly string marketLiabilityAddress
private readonly string clientId;
private readonly string secret;
public MarketLiability(ILogger<MarketLiability> logger, IEnvironmentVariableRetriever varRetriever)
{
this.logger = logger;
this.marketLiabilityAddress = varRetriever.GetEnvironmentVariable("baseUrl");
this.serviceAddress = varRetriever.GetEnvironmentVariable("ServiceAddress");
this.clientId = varRetriever.GetEnvironmentVariable("ClientId");
this.secret = varRetriever.GetEnvironmentVariable("Secret");
}
public async Task<Quotes> GetMarketQuotes()
{
this.logger.LogInformation("Fetching all market quotes");
var uri = "quotes";
var token = GetAccessToken();
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, $"{this.marketLiabilityAddress}/{uri}");
request.Headers.Add("Authorization", $"Bearer {token}");
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<Quotes>(content);
return result;
}
}
public interface IMarketLiability
{
Task<Quotes> GetMarketQuotes();
}
I have an integration test that works, but I need to unit test it with fake Urls, clientid, secret
[Fact]
public void TestMockConnection()
{
// Arrange
environmentVariableRetrieverMock = new Mock<IEnvironmentVariableRetriever>();
this.environmentVariableRetrieverMock
.Setup(x => x.GetEnvironmentVariable("baseUrl"))
.Returns("some actual url");
this.environmentVariableRetrieverMock
.Setup(x => x.GetEnvironmentVariable("ServiceAddress"))
.Returns("some actual url");
this.environmentVariableRetrieverMock
.Setup(x => x.GetEnvironmentVariable("ClientId"))
.Returns("ClientId");
this.environmentVariableRetrieverMock
.Setup(x => x.GetEnvironmentVariable("Secret"))
.Returns("Secret");
var marketLiability = new MarketLiability(this.loggerMock.Object, this.environmentVariableRetrieverMock.Object);
// Act
var response = await marketLiability.GetMarketQuotes();
// Assert
response.Should().NotBeNull();
response.Should().BeOfType<Quotes>();
}
How can I unit test this without having to put the actual Url. I tried a fake Url and when it come to the line var response = await marketLiability.GetMarketQuotes();
it fails with an invalid Url error.
Any ideas?
Thanks