Replies: 1 comment 2 replies
-
Hey @grimmerj, you get the error, because you never provide a The following code should only draw a picture, I don't guarantee that it really works (will check that later when I have a proper setup) public void Test()
{
using var ctx = new TestContext();
var factoryMock = new Mock<IHttpClientFactory>();
ctx.Services.AddScoped(_ => factoryMock.Object);
var mockClient = GetMockHttpClient();
factoryMock.Setup(f => f.CreateClient()).Returns(mockClient);
// ...
}
public static HttpClient GetMockHttpClient()
{
var mockHttpHandler = new MockHttpMessageHandler();
var httpClient = mockHttpHandler.ToHttpClient();
httpClient.BaseAddress = new Uri("http://localhost");
return httpClient;
} But I would leverage a different approach: Don't have any |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am new to Blazor and I am currently trying to test a razor component in my project.
My problem is that my test fails when I try to mock an http client. Without the line "@Inject IHttpClientFactory _httpClientFactory" in my razor component, the test does not fail whether the code for mocking the http client is in the test file or not. But when I add the line "@Inject IHttpClientFactory _httpClientFactory" in my razor file, the test fails.
What could be the problem and how can I fix it?
I want to use IHttpClientFactory for the tests because I don't want to instantiate a new http client for each component.
Is this possible?
With "@Inject HttpClient httpclient" in the razor component, we have got it to work, but would like to avoid using an explicit httpclient. Rather, we would like to use the IHttpClientFactory, as this is how the production code will be deployed.
Error:
Stacktrace:
c# unit test file:
package:
Beta Was this translation helpful? Give feedback.
All reactions