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
Testing - HttpTestingController throws errors for valid JSON #20690
Comments
|
I'm encounter the same issue with angular in version 4.4.6. Is there a workaround? |
|
Any update on this issue? I have the same problem. My application run Angular 5.2.1. |
|
I found a workaround. But i'm not sure if it is the best solution. Here what I did until they fix it. |
|
Your workaround gave me this: |
|
Can you add a snippet of your code? |
The flush method in TestRequest can return a boolean as this is a valid json response. Closes angular#20690
|
Sure... It is like this: |
The flush method in TestRequest can return a boolean as this is a valid json response. Closes angular#20690
The flush method in TestRequest can return a boolean as this is a valid json response. Closes angular#20690
The flush method in TestRequest can return a boolean as this is a valid json response. Closes angular#20690
The flush method in TestRequest can return a boolean as this is a valid json response. Closes angular#20690
|
submitted a PR, but i doubt i did it right... one of the travis builds doesnt like me |
|
this is super annoying. please get fix in soon |
|
work-a-round: const req = httpMock.expectOne('some-url');
req.event(new HttpResponse<boolean>({body: true}));instead of req.flush(true) |
|
The above workaround simply masks the issue, since no response is actually resolved without calling 'flush' or 'error'. The reason it will appear to succeed is simply due to the specific nature of the testing scenario, for example: As stated in the docs, 'event' is used for sending HttpEvent responses for things like progress updates, and as such doesn't appear to have much use unless 'flush' or 'error' is used to call 'complete' or 'error' on the underlying observable. The solution I used was simply to use a 'truthy' value in place of the boolean. Any underlying checks against a boolean value can usually be resolved within conditional statements this way, and so any logic checks in the underlying method code should be covered in this scenario: Changing the one to zero in the above test causes the test to fail, and as such achieves the exact same behaviour as I would have expected from using a boolean value in the body of the response. |
|
Any updates on if this is getting fixed? |
|
I did not manage to find a solution with above comments.
|
|
Maybe this will help anybody: |
|
I'm still seeing this issue. Any update? |
|
Angular 8... same issue :-( |
|
Should just be able to write: |
|
yes, this still is a problem. I could not get @adamlarner's solution to work (above) but was able to tweak with flushing a 0 or 1 and then having the test verify as: I am not sure this will pass muster, but would like to see the httpTestingController handle this more cleanly as well. |
|
I also couldn't get @adamlarner's solution to work, but a simple modification of his workaround is to use double-not Another alternative is to flush a string of 'true' (or 'false') |
|
Confirmed as reported, and this should be an easy fix. Definitely an opportunity for a community PR (I see there is one, but it's a few years old - hopefully @jdell64 is still around!) |
boolean is a valid json but at present we cannot test Http request using boolean added support for boolean requests Fixes angular#20690
boolean is a valid json but at present we cannot test Http request using boolean added support for boolean requests Fixes angular#20690
boolean is a valid json but at present we cannot test Http request using boolean added support for boolean requests Fixes angular#20690
boolean is a valid json but at present we cannot test Http request using boolean added support for boolean requests Fixes angular#20690
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
HttpTestingControllerthrows exception for valid JSON.Failed: Automatic conversion to JSON is not supported for response type.error(see https://github.com/angular/angular/blob/master/packages/common/http/testing/src/request.ts#L159)
But according to http://www.json.org/ valid JSON is:
Expected behavior
It should not throw exception and use value as JSON response of http call.
Minimal reproduction of the problem with instructions
Environment
The text was updated successfully, but these errors were encountered: