-
-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When creating HttpTest in async setup, real calls made in unit test #375
Comments
I'm pretty sure your compiler gives you warnings about that async function as you await nothing in it ;). And I'm also pretty sure that that might be the problem |
Firstly, the compiler warning is that the the method will run synchronously, implying it should be equivalent to the second method, and doesn't mention anything about not being in test mode. Secondly, adding "await Task.Delay(100);" to the method doesn't change the test error but removes the warning. Finally, as background, in the actual code that first produced the error, the setup method was part of an integration test class and had the [TestInitialize] attribute to run it automatically. It contained code to set up database client and secure storage classes which require async calls. The code above is the minimum to reproduce the error. The real tests produced strange 401 errors and after investigation I realised, embarrassingly, I had been repeatedly pinging the actual REST service with garbage username, password and api keys. The work-around is fairly obviously to create two setups, one async and one not and call them in each test. However, it took an hour or more to strip the original code down until I could isolate what was breaking it. |
@Rippletank Thank you for reporting this, you're definitely on to something here. In order for @TomBruyneel As as side note, his example is called an MVCE. Presumably his real code does something asynchronous in that method, but it's not relevant to repro this bug. It's perfect the way it is. |
Many thanks again. Yes, as you say it is straightforward to work around, once you know and it does work really well for testing. |
I would be interested in this as well. We have async fixture to setup auth tokens before the test and it often fails with the same error. If you then rerun failed tests only, it works just fine. |
I've confirmed that In the mean time, the work-around is pretty simple: if you want a class-level HttpTest instance, instantiate it in the constructor or synchronous setup, not async setup. |
I don't believe this is fixable. As proven here and explained here, a caller can set a value on That's what's happening here. Flurl uses The work-around is pretty simple: create the |
I evaluated this work-around and concluded it's not applicable here. Any way you look at it, something would still need to set a value on |
Setting |
I've found what I think is a bug in HttpTest. When the HttpTest instance is setup inside an async call, calls to GetAsync actually try to call the real url. There is a test project attached below. The async setup is needed for some db calls.
Here is a test example done in MSTest:
This results in this error:
Flurl.Http.FlurlHttpException: Call failed. No such host is known GET https://aaa.bbbbb.ccccc/api ---> System.Net.Http.HttpRequestException: No such host is known ---> System.Net.Sockets.SocketException: No such host is known
If the setup method is changed to a non-async method (commented out line above), there is no error.
It is possible to work around once the existence of the error is known but I'm sure this isn't intended behaviour so I'm bringing it to your attention.
Thanks for a great library!
Nick
TestProject.zip
The text was updated successfully, but these errors were encountered: