I have a question on how to test / mock out the IFlurlClientFactory and IFlurlClient. I'm registering my service (as a singleton eventually via DI), which uses the IFlurlClientFactory in a following way:
public ImageRetrievalServiceClientV1(
IFlurlClientFactory flurlClientFactory
)
{
_flurlClient = flurlClientFactory.Get(someUrl);
}
A method is defined in the ImageRetrievalServiceClientV1 where I use the _flurlClient
as instantiated above, as follows:
await _flurlClient.Request("some", "path", "segements")
.WithOAuthBearerToken(token)
.PostJsonAsync(someObject);
Question: Is there a nice way that can I mock out the IFlurlClientFactory
and IFlurlClient
using the Moq testing framework? Thanks in advance for the answers.
Regards, Tom
[UPDATE]: I misunderstood the whole point of the HttpTest. Fortunately the docs are clarifying things which I apparently missed in the first instance... -_-'
What I just needed to do was to instantiate HttpTest in my unittest. For example if I want my sut to throw a FlurlHttpException
I just need to tell the httpTest instance to respond with, for example, a server error:
using var httpTest = new HttpTest();
//httpTest.RespondWith("some response body");
httpTest.RespondWith("server error", 500);
Instantiate a new PerBaseUrlFlurlClientFactory
:
var sut = new ImageRetrievalServiceClientV1(new PerBaseUrlFlurlClientFactory(),....);
Subsequent calls on the sut which trigger the calls are mocked out because of the setup of the HttpTest
. This works like a charm!