Skip to content

Commit 7a9a968

Browse files
author
Your Name
committed
Angular Testing Course
1 parent 4eabb59 commit 7a9a968

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/app/courses/services/courses.service.ts

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export class CoursesService {
2626
);
2727
}
2828

29-
3029
saveCourse(courseId:number, changes: Partial<Course>): Observable<Course> {
3130
return this.http.put<Course>(`/api/courses/${courseId}`, changes);
3231
}

src/app/courses/services/courses.services.spec.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {TestBed} from '@angular/core/testing';
33
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
44
import {COURSES} from '../../../../server/db-data';
55
import {Course} from '../model/course';
6+
import {HttpErrorResponse} from '@angular/common/http';
67

78

89
describe('CoursesService', () => {
@@ -69,8 +70,6 @@ describe('CoursesService', () => {
6970

7071
});
7172

72-
73-
7473
it('should save the course data', () => {
7574

7675
const changes :Partial<Course> =
@@ -97,6 +96,27 @@ describe('CoursesService', () => {
9796

9897
});
9998

99+
it('should give an error if save course fails', () => {
100+
101+
const changes :Partial<Course> =
102+
{titles:{description: 'Testing Course'}};
103+
104+
coursesService.saveCourse(12, changes)
105+
.subscribe(
106+
() => fail("the save course operation should have failed"),
107+
108+
(error: HttpErrorResponse) => {
109+
expect(error.status).toBe(500);
110+
}
111+
);
112+
113+
const req = httpTestingController.expectOne('/api/courses/12');
114+
115+
expect(req.request.method).toEqual("PUT");
116+
117+
req.flush('Save course failed', {status:500,
118+
statusText:'Internal Server Error'});
119+
});
100120

101121

102122
afterEach(() => {
@@ -108,3 +128,15 @@ describe('CoursesService', () => {
108128

109129

110130

131+
132+
133+
134+
135+
136+
137+
138+
139+
140+
141+
142+

0 commit comments

Comments
 (0)