如何测试使用构造函数参数的服务?我正试图通过docs了解测试,但遇到了麻烦。我有一项服务location-search.service.ts
:
import { Injectable } from '@angular/core';
import { Http, Response, Jsonp } from '@angular/http';
import { Observable } from 'rxjs';
import { Location } from '../models/location';
@Injectable()
export class LocationSearchService {
constructor(private jsonp: Jsonp) {}
search(term: string): Observable<Location[]> {
return this.jsonp
.get(/* api url */)
.map((r: Response) => r.json().RESULTS as Location[]);
}
}
现在,当我写location-search.spec.ts
时,如何正确地将jsonp
注入服务? e.g。
describe('LocationSearchService', () => {
let service: LocationSearchService;
// how do I initialize LocationSearchService with Jsonp in this case?
beforeEach(() => { service = new LocationSearchService(); });
it('#search should return observable value', () => {
expect(service.search('term').toBe('observable value');
done();
});
});