Is there are way to verify the request object passed to HttpClient.PostAsJsonAsync. The request object is constructed inside the method to be unit tested. Therefore I need to verify that the request object is constructed with correct values.
I tried the following and I am expecting to verify the searchrequest object by comparing the values as shown below
` public void Test(){
<code> //Arrange
//ACT
await _myservice.someMethodToTest(criteria);
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1), // we expected a single external request
ItExpr.Is<HttpRequestMessage>(req =>
req.Method == HttpMethod.Post // we expected a POST request
),
ItExpr.IsAny<CancellationToken>(),
ItExpr.Is<SearchRequest>(r => r.PublishedDateStart == publishedDateStart) // this doesn't work
);
}
//Method to be tested
public void someMethodToTest(Criteria criteria){
// build some complext request from criteria
var searchRequest = new searchRequest{
}
var searchResponse = await httpClient.PostAsJsonAsync(url, searchRequest);
//other code
}``
</code>
<code> //Arrange
//ACT
await _myservice.someMethodToTest(criteria);
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1), // we expected a single external request
ItExpr.Is<HttpRequestMessage>(req =>
req.Method == HttpMethod.Post // we expected a POST request
),
ItExpr.IsAny<CancellationToken>(),
ItExpr.Is<SearchRequest>(r => r.PublishedDateStart == publishedDateStart) // this doesn't work
);
}
//Method to be tested
public void someMethodToTest(Criteria criteria){
// build some complext request from criteria
var searchRequest = new searchRequest{
}
var searchResponse = await httpClient.PostAsJsonAsync(url, searchRequest);
//other code
}``
</code>
//Arrange
//ACT
await _myservice.someMethodToTest(criteria);
handlerMock.Protected().Verify(
"SendAsync",
Times.Exactly(1), // we expected a single external request
ItExpr.Is<HttpRequestMessage>(req =>
req.Method == HttpMethod.Post // we expected a POST request
),
ItExpr.IsAny<CancellationToken>(),
ItExpr.Is<SearchRequest>(r => r.PublishedDateStart == publishedDateStart) // this doesn't work
);
}
//Method to be tested
public void someMethodToTest(Criteria criteria){
// build some complext request from criteria
var searchRequest = new searchRequest{
}
var searchResponse = await httpClient.PostAsJsonAsync(url, searchRequest);
//other code
}``
New contributor
arjun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.