I am trying to figure out how to use FakeItEasy with HttpClient, given the following code:
public Foo(string key, HttpClient httpClient = null) { .. } public void DoGet() { .... if (_httpClient == null) { _httpClient = new HttpClient(); } var response = _httpClient.GetAsync("user/1); } public void DoPost(foo Foo) { if (_httpClient == null) { _httpClient = new HttpClient(); } var formData = new Dictionary<string, string> { {"Name", "Joe smith"}, {"Age", "40"} }; var response = _httpClient.PostAsync("user", new FormUrlEncodedContent(formData)); }
So I'm not sure how to use FakeItEasy to fake the HttpClient GetAsync and PostAsync .
production code will not pass in HttpClient, but unit test will pass in a fake instance made by FakeItEasy.
eg.
[Fact] public void GivenBlah_DoGet_DoesSomething() {
UPDATE:
I understand that FiE (and most mocking packages) works on interfaces or virtual methods. So, for this question, let's just assume that the GetAsync and PostAsync are virtual ... please :)
Pure.Krome
source share