Unit testing a synchronous service
Angular 2 service types are essentially classes designated for injectability. They are easy to test since you have a great deal of control over how and where they are provided, and consequently, how many instances you'll be able to create. Therefore, tests for services will exist largely as they would for any normal TypeScript class.
It'll be better if you are familiar with the content of the first few recipes of this chapter before you proceed further.
Note
The code, links, and a live example related to this recipe are available at http://ngcookbook.herokuapp.com/3107.
Getting ready
Suppose you want to build a "magic eight ball" service. Begin with the following code, with added comments for clarity:
[src/app/magic-eight-ball.service.ts] import {Injectable} from '@angular/core'; @Injectable() export class MagicEightBallService { private values:Array<string>; private lastIndex:number; constructor() { ...