Post

Angular 2 Services Testing Template

Here’s a simple template for testing Angular 2 services.

If the HttpModule portions are uncommented and used, it will make actual wire calls. To use mock calls instead, see Angular 2 MockBackend Service Testing Template Using TestBed instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { async, inject, TestBed } from '@angular/core/testing';
// import { HttpModule } from '@angular/http';

import { SomeService } from './some.service';

describe('SomeService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        SomeService
      ],
      imports: [
        // HttpModule
      ]
    });
  });

  it('should construct', async(inject([SomeService], (service) => {
    expect(service).toBeDefined();
  })));
});

Also, if you’re looking for a component testing template, see Angular 2 Component Testing Template Using TestBed.

This post is licensed under CC BY 4.0 by the author.