0

I have Integration Tests failing when run at once, but pass if run separately.

The tests are for Service 1 which initializes dependent Service 2. Service 2 receives HttpClientHandler through DI where it sets default behavior (AllowAutoRedirect property).

Due to a test scenario I have to use HttpClientHandler to create the HttpClient in Service 2. (To be specific the call through HttpClientHandler is intercepted in the Integration tests where response content is changed. There is no way to do this through HttpWebRequest and HttpClientFactory. Hence the default behaviour for HttpClientHandler is in Service 2's constructor).

Why am I getting this error when tests are run at once? What is happening twice? The tests work fine separately which is honestly baffling.

    System.InvalidOperationException : This instance has already started one or more requests. Properties can only be modified before sending the first request.
  Stack Trace: 
    SocketsHttpHandler.CheckDisposedOrStarted()
    HttpClientHandler.set_AllowAutoRedirect(Boolean value)

Have I made a design sin somewhere?

aka17
  • 73
  • 1
  • 1
  • 9
  • how do i pass HttpClientHandler from my test to my service correctly with options? It would fix this scenario IMO. I have inherited HttpClientHandler in the test and override its SendAsync method. but setting AutoRedirect property in the constructor still gives this error – aka17 Mar 25 '21 at 18:46
  • Could you please be more concrete? It is unclear why do you even bother about `HttpClientHandler` in case Integration testing. In case integration testing if you have an http based service dependency then mock it with [WireMock.Net](https://github.com/WireMock-Net/WireMock.Net) or with [Stubbery](https://github.com/markvincze/Stubbery) – Peter Csala Mar 30 '21 at 11:57

0 Answers0