npm test
を実行しているとき、私はこのエラーを取得:このangular2シード使用Angular2 HTTPサービスをMockBackendで正しくテストする方法は?
src/client/app/shared/services/scientist.service.spec.ts(20,7): error TS2345: Argument of type 'Function' is not assignable to parameter of type '(done:() => void) => void'.
Type 'Function' provides no match for the signature '(done:() => void): void'
I'am:https://github.com/mgechev/angular2-seedを、私はこのブログに基づいて、HTTPからデータを取得するために、科学者の名前リストのサービスを変更:http://chariotsolutions.com/blog/post/testing-http-services-angular-2-jasmine/。
私はasScientist
とasScientists
のメソッドを使用してScientist
クラスを作成し、jsonからインスタンスを作成しました。
scientist.service.ts:
import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
import {Scientist} from './scientist';
@Injectable()
export class ScientistService {
private url = 'http://path/to/scientists/';
constructor(private http:Http) {
}
get():Observable<Scientist[]> {
return this.http.get(this.url)
.map((res:Response) => {
return Scientist.asScientists(res.json());
});
}
}
scientist.service.spec.ts:
import {provide} from 'angular2/core';
import {beforeEachProviders, inject} from 'angular2/testing';
import {XHRBackend, ResponseOptions, Response} from 'angular2/http';
import {MockBackend, MockConnection} from 'angular2/src/http/backends/mock_backend';
import {Scientist} from './scientist';
import {ScientistService} from './scientist.service.ts';
export function main() {
describe('Scientist Service',() => {
beforeEachProviders(() => {
return [HTTP_PROVIDERS, provide(XHRBackend, {useClass: MockBackend}), ScientistService];
});
it('should get the list of scientists',
// ##### Line below causes the error #####
inject([XHRBackend, ScientistService], (mockBackend:MockBackend, scientistService:ScientistService) => {
mockBackend.connections.subscribe(
(connection:MockConnection) => {
// Haven't changed these yet since I want to make the test pass first
connection.mockRespond(new Response(
new ResponseOptions({
body: [
{
id: 26,
contentRendered: '<p><b>Hi there</b></p>',
contentMarkdown: '*Hi there*'
}]
}
)));
});
scientistService.get().subscribe((scientists:Scientist[]) => {
expect(scientists.length).toBe(1);
//expect(scientists[0].id).toBe(26);
//expect(data[0].contentMarkdown).toBe('*Hi there*');
},
(error:any) => {
// we can call a failure case here...
fail(error);
});
}));
});
}
構文エラーではなく、非常に明白なので、あらゆる種類ののようです助けていただければ幸いです!
構文エラーとは何ですか? – theUtherSide
これは質問の説明の先頭です: 'Type 'Function'は' scientist.service.spec.ts(20,7) 'の署名( 'done :()=> void):void' – fips