forked from juristr/angular-testing-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.service.fake-call.spec.ts
55 lines (44 loc) · 1.34 KB
/
remote.service.fake-call.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* tslint:disable:no-unused-variable */
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing';
import { of } from 'rxjs/observable/of';
import { TestBed } from '@angular/core/testing';
import { RemoteService } from './remote.service';
describe('RemoteService (fake call with Jasmine)', () => {
let service: RemoteService;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
// providers: [{ provide: RemoteService, useValue: jasmineSpy }]
providers: [RemoteService]
});
// inject the service
service = TestBed.get(RemoteService);
});
it('should have a service instance', () => {
expect(service).toBeDefined();
});
it('should return the mocked data in the subscribe', () => {
const spy = spyOn(service, 'fetchViaHttp').and.returnValue(
of({
name: 'Juri'
})
);
// act
service.fetchViaHttp().subscribe(data => {
expect(data.name).toBe('Juri');
});
// assert
expect(spy).toHaveBeenCalled();
});
it('should not invoke the error throwing function since we mocked it', () => {
const emptyFn = () => {};
const spy = spyOn(service, 'throwingError').and.callFake(emptyFn);
// act
service.throwingError();
// assert
expect(spy).toHaveBeenCalled();
});
});