I have a service-class that gets a FlurlHttpClient
injected in the constructor.
It has a public method which makes a call using the httpClient
and then parses the response and returns a class. So basically I want to fake the response from the API and test the parse-method, which is private.
How do I unit test this? I am using NUnit and FakeItEasy for testing. So far I got this, but how do I ensure that the ParseResult
-method gets tested with the faked result from the API?
Code so far:
Unit-test:
[Test]
public void GetShipmentData_SuccessfullTracking_ReturnsValidEntity() {
//Fake the service-class
var sut = A.Fake<IApiClient>();
using (var httpTest = new HttpTest()) {
httpTest.RespondWithJson(GetJsonFromFile("../../../Assets/SuccessfullApiTrackingResponse.json"), 200);
//This does not actually run the function on the service-class.
var response = sut.TrackShipmentUsingReferenceNumber("fakeReferenceNumber");
Assert.IsTrue(response.SuccessfullShipmentTracking);
Assert.IsNotNull(response.ApiResponseActivity);
}
}
Api-class:
public class ApiClient : IApiClient {
readonly ILogger _logger;
private readonly IFlurlClient _httpClient;
public ApiClient(IFlurlClientFactory flurlClientFac) {
_httpClient = flurlClientFac.Get(ApiClientConfiguration.BaseAdress);
}
public ApiResponse TrackShipmentUsingReferenceNumber(string referenceNumber) {
var request = GenerateApiRequestUsingReferenceNumber(referenceNumber);
var response = _httpClient.Request("Track").PostJsonAsync(request).ReceiveString();
return ParseResult(response.Result);
}
private ApiResponse ParseResult(string input) {
//Shortened
return = JObject.Parse<ApiResponse>(input);
}
}