So there is the method that executes an API Call
public async Task<ClientTotalResponse> ExecuteAsync(ClientTotalRequest totalRequest)
{
var path = $"{_options.Value.BaseUrl}/" +
$"{_options.Value.TotalReportEndpoint}?" + "date=" +
$"{_options.Value.DateQueryParameterName = totalRequest.ReportDate.ToString(_options.Value.DateQueryFormat)}&" + "iso=" +
$"{_options.Value.IsoQueryParameterName = totalRequest.CountryIsoCode}";
HttpResponseMessage response = await _totalReportClient.GetAsync(path);
if (response.IsSuccessStatusCode)
{
var responseData = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<ClientTotalResponse>(responseData);
_logger.LogInformation($"Request is {JsonSerializer.Serialize(totalRequest)}.", LogLevel.Information);
_logger.LogInformation($"Response is {JsonSerializer.Serialize(result)}.", LogLevel.Information);
return result;
}
_logger.LogError("Failed to execute report request.", LogLevel.Error);
return null;
}
This is the unit test method
[Test]
public async Task On_ExecuteAsync_Should_Use_Logger_If_OK()
{
var response = new HttpResponseMessage
{
StatusCode = System.Net.HttpStatusCode.OK,
Content = new StringContent("{}")
};
_httpMessageHandlerMock.Protected()
.Setup<Task<HttpResponseMessage>>(
"SendAsync",
ItExpr.IsAny<HttpRequestMessage>(),
ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(response);
var result = await _client.ExecuteAsync(_reportTotalRequest);
_loggerMock
.VerifyLogging($"Request is {JsonSerializer.Serialize(_reportTotalRequest)}.", LogLevel.Information)
.VerifyLogging($"Response is {JsonSerializer.Serialize(result)}.", LogLevel.Information);
}
So the problem it following:
Message:
System.FormatException : Format string can be only "G", "g", "X", "x", "F", "f", "D" or "d".
Stack Trace:
Enum.ToString(String format)
Enum.ToString(String format, IFormatProvider provider)
ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
String.Format(IFormatProvider provider, String format, Object[] args)
FormattedLogValues.ToString()
I know what problem means, it means that i am giving string value to enum field but i am not using Enum, or using and i don't know where.
What I have tried:
I have tried find solution in following classes parameters:
public record ClientTotalRequest
{
public string CountryIsoCode { get; init; }
public DateTime ReportDate { get; init; }
};
I thought that the problem was converting "ReportDate" into new format(located in ExecuteAsync method while making path) which is "yyyy-MM-dd" but it isn't problem.
Do you have any ideas what is the problem? or am i even using enum and have no idea?