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] ...
Get Angular 2 Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.