Skip to content

Instantly share code, notes, and snippets.

@MatLang
Created June 19, 2019 10:34
Show Gist options
  • Save MatLang/623ad45841249a535011b70d66345ab5 to your computer and use it in GitHub Desktop.
Save MatLang/623ad45841249a535011b70d66345ab5 to your computer and use it in GitHub Desktop.
Service testing #testing
describe('CoursesService', () => {
let coursesService: CoursesService,
httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule
],
providers: [
CoursesService
]
});
coursesService = TestBed.get(CoursesService),
httpTestingController = TestBed.get(HttpTestingController);
});
it('should retrieve all courses', () => {
coursesService.findAllCourses()
.subscribe(courses => {
expect(courses).toBeTruthy('No courses returned');
expect(courses.length).toBe(12,
"incorrect number of courses");
const course = courses.find(course => course.id == 12);
expect(course.titles.description).toBe(
"Angular Testing Course");
});
const req = httpTestingController.expectOne('/api/courses');
expect(req.request.method).toEqual("GET");
req.flush({payload: Object.values(COURSES)});
});
it('should find a course by id', () => {
coursesService.findCourseById(12)
.subscribe(course => {
expect(course).toBeTruthy();
expect(course.id).toBe(12);
});
const req = httpTestingController.expectOne('/api/courses/12');
expect(req.request.method).toEqual("GET");
req.flush(COURSES[12]);
});
it('should save the course data', () => {
const changes :Partial<Course> =
{titles:{description: 'Testing Course'}};
coursesService.saveCourse(12, changes)
.subscribe(course => {
expect(course.id).toBe(12);
});
const req = httpTestingController.expectOne('/api/courses/12');
expect(req.request.method).toEqual("PUT");
expect(req.request.body.titles.description)
.toEqual(changes.titles.description);
req.flush({
...COURSES[12],
...changes
})
});
it('should give an error if save course fails', () => {
const changes :Partial<Course> =
{titles:{description: 'Testing Course'}};
coursesService.saveCourse(12, changes)
.subscribe(
() => fail("the save course operation should have failed"),
(error: HttpErrorResponse) => {
expect(error.status).toBe(500);
}
);
const req = httpTestingController.expectOne('/api/courses/12');
expect(req.request.method).toEqual("PUT");
req.flush('Save course failed', {status:500,
statusText:'Internal Server Error'});
});
it('should find a list of lessons', () => {
coursesService.findLessons(12)
.subscribe(lessons => {
expect(lessons).toBeTruthy();
expect(lessons.length).toBe(3);
});
const req = httpTestingController.expectOne(
req => req.url == '/api/lessons');
expect(req.request.method).toEqual("GET");
expect(req.request.params.get("courseId")).toEqual("12");
expect(req.request.params.get("filter")).toEqual("");
expect(req.request.params.get("sortOrder")).toEqual("asc");
expect(req.request.params.get("pageNumber")).toEqual("0");
expect(req.request.params.get("pageSize")).toEqual("3");
req.flush({
payload: findLessonsForCourse(12).slice(0,3)
});
});
afterEach(() => {
httpTestingController.verify();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment