How to write a test case for Getasync() with mockdata return type is Task
Loading
How to write a test case for Getasync() with mockdata return type is Task
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit MohantyPosted Apr 27, 2023, 6:15 AM
Assuming you're using NUnit for testing. Here's an example of how to write a test case for a method that uses
GetAsync()to retrieve data from a service with aTaskreturn type:Now, let's write a test case that uses mock data to simulate the response from the service:
Here we're using
Moqto create a mockHttpMessageHandler, which we'll use to simulate the response from the service. We then set up the mock to return the expected response when theHttpClientmakes a call toSendAsync(). Finally, we call theGetDataAsync()method and verify that it returns the expected data.