|
1 | 1 | import { TestBed, getTestBed, inject } from '@angular/core/testing';
|
2 |
| -import { HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http'; |
| 2 | +import { HttpClient, HTTP_INTERCEPTORS, HttpErrorResponse } from '@angular/common/http'; |
3 | 3 | import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
|
4 | 4 | import { Observable, of } from 'rxjs';
|
5 | 5 |
|
@@ -90,13 +90,13 @@ describe('UserService', () => {
|
90 | 90 | it('should throw with an error message when API returns an error',
|
91 | 91 | inject([HttpClient, HttpTestingController], (http: HttpClient, httpMock: HttpTestingController) => {
|
92 | 92 | const userService = getTestBed().get(UserService);
|
93 |
| - userService.getUsers().subscribe({ |
94 |
| - error(actualError) { |
95 |
| - expect(of(actualError)).toBeTruthy(); |
96 |
| - expect(actualError).not.toBeNull(); |
97 |
| - expect(actualError).not.toBeUndefined(); |
| 93 | + userService.getUsers().subscribe( |
| 94 | + response => fail('should have failed with 500 status'), |
| 95 | + error => { |
| 96 | + expect(error).toBeTruthy(); |
| 97 | + expect(error.status).toEqual(500); |
98 | 98 | }
|
99 |
| - }); |
| 99 | + ); |
100 | 100 |
|
101 | 101 | const req = httpMock.expectOne(userService.apiEndpoint);
|
102 | 102 | expect(req.request.method).toEqual('GET');
|
@@ -151,12 +151,13 @@ describe('UserService', () => {
|
151 | 151 |
|
152 | 152 | const userService = getTestBed().get(UserService);
|
153 | 153 | userService.addUser(expectedResult)
|
154 |
| - .subscribe({ |
155 |
| - error(actualError) { |
156 |
| - expect(of(actualError)).toBeTruthy(); |
157 |
| - expect(actualError).toBeTruthy(); |
| 154 | + .subscribe( |
| 155 | + response => fail('should fail with status 500 error'), |
| 156 | + (error: HttpErrorResponse) => { |
| 157 | + expect(error).toBeTruthy(); |
| 158 | + expect(error.status).toEqual(500); |
158 | 159 | }
|
159 |
| - }); |
| 160 | + ); |
160 | 161 |
|
161 | 162 | const req = httpMock.expectOne(r => r.url === userService.apiEndpoint && r.headers.has('Content-Type'));
|
162 | 163 | expect(req.request.method).toEqual('POST');
|
|
0 commit comments