Testing services

Let's create a sample service. Our service has only one method that returns an array of the available users for the application:

//a simple serviceexport class UsersService {get() {return ['Ken', 'Mark', 'Chris'];}}

We instantiate the service using the beforeEach method. This is a good practice even if we only have one spec. We are checking each individual user and the total count:

describe('Service: UsersService', () => {let service;beforeEach(() => TestBed.configureTestingModule({providers: [ UsersService ]}));beforeEach(inject([UsersService], s => {service = s;}));it('should return available users', () => {let users = service.get();expect(users).toContain('en');expect(users).toContain('es');expect(users).toContain('fr'); ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.