I have this class:
public class HttpService : IHttpService
{
private HttpClient _httpClient;
public HttpService()
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("https://localhost:5001");
}
public async Task<HttpResponseMessage> PostDataAsync(string endPoint, object data)
{
return await _httpClient.PostAsync($"/api/chat/{endPoint}",
new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"));
}
public async Task<UserModel> PostLoginCredentialsAsync(UserCredentials userCredentials)
{
var response = await PostDataAsync("Login", userCredentials).ConfigureAwait(false);
var jsonResponseData = await response.Content.ReadAsStringAsync();
var userResponseModel = JsonConvert.DeserializeObject<UserResponseModel>(jsonResponseData);
if (response.StatusCode != HttpStatusCode.OK) throw new LoginException(userResponseModel.Message);
return userResponseModel.Payload;
}
}
I am trying to test the method PostLoginCredentialsAsync and mock the method PostDataAsync.
Now, I am not really sure what's the best way to mock it. I don't want to make PostDataAsync virtual and mock it. I could try to make another interface wrapping HttpClient, but I am not sure how that would look without changing the IHttpService interface.