forked from juristr/angular-testing-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.service.spec.ts
More file actions
55 lines (44 loc) · 1.51 KB
/
remote.service.spec.ts
File metadata and controls
55 lines (44 loc) · 1.51 KB
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 { TestBed, async, inject } from '@angular/core/testing';
import { Injectable } from '@angular/core';
import { Http, HttpModule, XHRBackend, Response, ResponseOptions } from '@angular/http';
import {MockBackend, MockConnection} from '@angular/http/testing';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
class RemoteService {
constructor(private http: Http) {}
fetchViaHttp() : Observable<any> {
return this.http
.get('/somendpoint/people.json')
.map(x => x.json());
}
}
describe('RemoteService', () => {
let service: RemoteService;
let mockBackend;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
RemoteService,
MockBackend,
{ provide: XHRBackend, useClass: MockBackend }
]
});
// inject the service
service = TestBed.get(RemoteService);
mockBackend = TestBed.get(MockBackend);
});
it('should have a service instance', () => {
expect(service).toBeDefined();
});
it('should return the json', async(() => {
mockBackend.connections.subscribe((conn:MockConnection) => {
conn.mockRespond(new Response(new ResponseOptions('{ "name": "Juri" }')));
});
service.fetchViaHttp().subscribe((data) => {
expect(data.name).toBe('Juri');
});
}));
});