2016-05-11 11 views
2

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/

私はasScientistasScientistsのメソッドを使用して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); 
      }); 
     })); 
    }); 
} 

構文エラーではなく、非常に明白なので、あらゆる種類ののようです助けていただければ幸いです!

+0

構文エラーとは何ですか? – theUtherSide

+0

これは質問の説明の先頭です: 'Type 'Function'は' scientist.service.spec.ts(20,7) 'の署名( 'done :()=> void):void' – fips

答えて

1

おそらく、すべてのテストメソッドをangle2/testingモジュールからインポートする必要があります。このよう

import {it, beforeEachProviders, describe, expect, beforeEach, inject, injectAsync} from 'angular2/testing'; 

あなたのコードで、私はあなただけ "beforeEachProviders" をインポートし、 "注入" を見たので。

+0

Angularの最新バージョンを更新するだけです。 '@ angular/core/testing'からimport {async、describe、it、expect、inject、beforeEach、beforeEachProviders} –

関連する問題