I need to write unit tests (and most importantly, i'm being asked to get code coverage) for this controller.
public class MyController : Controller
{
public async Task<ActionResult> Index()
{
ViewBag.Title = "Home Page";
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(ConfigurationManager.AppSettings["MyCustomAPI"]);
var tasks = JsonConvert.DeserializeObject<List<MyObject>>(json);
return View(tasks);
}
}
I've been reading ideas for two days about this but i can't wrap my head around it.
I've been considering injecting the HttpClient like this:
private readonly HttpClient _httpClient;
public TasksController(HttpClient httpClient)
{
_httpClient = httpClient;
}
But it's probably not a good idea to pass an HttpClient in the controller constructor.
Then i've been following this guide that uses FakeItEasy but i couldn't adapt this solution to my requirements.
How should i proceed for testing this?