Skip to content
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

Problems mocking MockConnection error response #12857

Closed
mildfuzz opened this issue Nov 14, 2016 · 8 comments
Closed

Problems mocking MockConnection error response #12857

mildfuzz opened this issue Nov 14, 2016 · 8 comments
Labels
area: testing Issues related to Angular testing features, such as TestBed freq2: medium type: bug/fix
Projects
Milestone

Comments

@mildfuzz
Copy link

mildfuzz commented Nov 14, 2016

I'm submitting a ... (check one with "x")

[ x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
Test Repo - https://github.com/mildfuzz/angular-mockError

I am unable to test predicted HTTP error responses. Mockbackend can either send a response with an error code (which my app treats as a successful response in the test environment, but as an error in real world)

I am unable to send status codes with an error response in my tests, so I can not test any error handling my application might implements.

Expected behavior
at least one of the defined test cases passes

Minimal reproduction of the problem with instructions
Test Repo - https://github.com/mildfuzz/angular-mockError

this is a re-opening of issue #7471, which was closed due to lack of repro steps.

What is the motivation / use case for changing the behavior?
To be able to test all responses

Please tell us about your environment:
OsX

  • Angular version: 2.0.X
    Angular 2.1.0

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
    Chrome 54

  • Language: TypeScript 2.0.3

  • Node (for AoT issues): node --version = 6.9.1

@chuckjaz chuckjaz added the area: testing Issues related to Angular testing features, such as TestBed label Nov 16, 2016
@fetis
Copy link

fetis commented Dec 12, 2016

I came across to the same problem. I would recommend to remove all redundant code from repository because it's not clear where to look in.

So briefly the problem looks next

http.get('some url')
  .map(value => {
    console.log('map');
   return value})
  .catch(() => console.log('catch')

and we have next mock response

backend.connections.subscribe((connection: MockConnection) => {
  connection.mockRespond(new Response(new ResponseOptions({
    status: 400
})))

so I expect it'll log catch as it happens with real backend. But actually it prints map all times.

The single way to fix it is to use connection.mockError()

@Ks89
Copy link

Ks89 commented Dec 28, 2016

Exactly the same problem also with the latest stable release of angular. Same situation of @fetis .

To fix this problem I used:

backend.connections.subscribe((c: MockConnection) => c.mockError(new Response(new ResponseOptions({ status: 404}))));

The solution described into the official testing example HERE is not working.

@Koslun
Copy link

Koslun commented Feb 7, 2017

Duplicate issue: #13690.

#7135 points out that the current working way requires a double cast in TypeScript (connection.mockError(response as any as Error);). As MockError only accepts Error while the expected behavior is to get a proper Response object. There is a fix for that in PR #13104 made by @DzmitryShylovich.

Other than the official testing documentation @Ks89 linked, there is at least one other error in the API documentation at https://angular.io/docs/ts/latest/api/http/testing/index/MockBackend-class.html . Where it also seems to be assumed that responses with code 404 should result in a thrown error rather than a regular response.

Fixing this to generate the expected error might break some folks' tests, but think most or all of those tests were wrong to begin with.

@royling
Copy link
Contributor

royling commented May 9, 2017

the current working way requires a double cast in TypeScript (connection.mockError(response as any as Error);). As MockError only accepts Error while the expected behavior is to get a proper Response object.

I tried to work around this problem by creating a class extending Response and implementing Error (type declared in types\node):

class ErrorResponse extends Response implements Error {
  name: any;
  message: any;
}

export function mockError(errorResponseOpts: any) {
  return new ErrorResponse(new ResponseOptions(errorResponseOpts));
}

and then mock error as:

mockConnection.mockError(mockError({status: 404}));

alxhub added a commit to alxhub/angular that referenced this issue Jun 6, 2017
Previously, MockConnection.mockError() only accepted an Error object.
However, in @angular/http, unsuccessful Responses are returned on the
error stream. Without this change, there is no easy way to mock an
unsuccessful Response.

This changes mockError() to also accept the Response type.

Fixes angular#12857
alxhub added a commit to alxhub/angular that referenced this issue Jun 7, 2017
Previously, MockConnection.mockError() only accepted an Error object.
However, in @angular/http, unsuccessful Responses are returned on the
error stream. Without this change, there is no easy way to mock an
unsuccessful Response.

This changes mockError() to also accept the Response type.

Fixes angular#12857
Fixes angular#7135
@alxhub alxhub moved this from Legacy Bugs to PR Pending in Http Jun 7, 2017
alxhub added a commit to alxhub/angular that referenced this issue Jun 7, 2017
Previously, MockConnection.mockError() only accepted an Error object.
However, in @angular/http, unsuccessful Responses are returned on the
error stream. Without this change, there is no easy way to mock an
unsuccessful Response.

This changes mockError() to also accept the Response type.

Fixes angular#12857
Fixes angular#7135
@JSchneidler
Copy link

JSchneidler commented Aug 15, 2017

Can we get an update on this? #13104 (the fix) was closed without an explanation.

I don't think these workarounds are a good long-term solution, though acceptable in the meantime if this is just a priority thing.

@alxhub
Copy link
Member

alxhub commented May 29, 2020

Closing as this pertains to @angular/http which is now obsolete.

@alxhub alxhub closed this as completed May 29, 2020
@nanoscz
Copy link

nanoscz commented Jun 29, 2020

Cannot find module '@angular/http/testing' or its corresponding type declarations. [Angular 9]

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: testing Issues related to Angular testing features, such as TestBed freq2: medium type: bug/fix
Projects
No open projects
Http
PR Pending
Development

Successfully merging a pull request may close this issue.