public MockHttpClientFactory() { _studentServiceHttpClient = new Lazy <HttpClient>(() => { var dg = new DG( new StudentGeneratorFactory(), new DataSchemeMapperConfig { UseCamelCase = true }); //Creates DataScheme based on DgStudentCollection attributes. DataScheme scheme = dg.GetFromType <DgStudentCollection>(); //Generates json random data. string jsonData = dg.GenerateJson(scheme); var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock.Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, //Responds with the generated data. Content = new StringContent(jsonData) }) .Verifiable(); return(new HttpClient(handlerMock.Object)); }); }