Given below was my c# code :
public async Task<bool>? CallValidationLayer(string id) {
try
{
var bodyContent = JsonConvert.SerializeObject(new { id = id });
var content = new StringContent(bodyContent, Encoding.UTF8, "application/json");
HttpResponseMessage validationResult = await _HttpClient.PostAsync(_ValidationLayerAPI, content);
if (validationResult.IsSuccessStatusCode)
{
return true;
}
return false;
}
catch(Exception ex)
{
return false;
}
}
For the above code i have write a test :
[Fact]
public async Task CallValidationLayerTrue()
{
//Arrange
string id = "test";
mockMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(
new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK
});
//Act
var result = await _mapper.CallValidationLayer(id);
//Assert
Assert.True(result);
}
for the above testing the “validationResult” was always null why
i am expecting response status code from “validationResult”