2017-10-17 7 views
0

私はAngular4の新機能で、構築した単純なサービスの単体テストを書く必要がありますが、どこから起動するのかはわかりません。Angular4 - サービスのテスト方法

import { Injectable } from '@angular/core'; 
import { Observable } from 'rxjs/Observable'; 
import { HttpClient } from '@angular/common/http'; 
import 'rxjs/add/operator/map'; 

import { KeyValuePair } from '../../models/keyvaluepair'; 
import { environment } from './../../../environments/environment'; 

// Lookups api wrapper class. 
@Injectable() 
export class LookupsService { 

    private servicegApiUrl = ''; 

    public constructor(private http : HttpClient) { 
     // Build the service api url, uring the environment lookups api url, plus controller name to reference. 
     this.servicegApiUrl = environment.webApiUrl + 'Lookups/'; 
    } 

    // Get the hubs from the web api. 
    public getHubs() : Observable<KeyValuePair<number>[]> { 
     // Carry out http get and map the result to the KeyValuePair number object. 
     return this.http.get<KeyValuePair<number>[]>(this.servicegApiUrl + 'Hubs').map(res => { return res; }); 
    } 
} 

私は方法を知っている私のgetHubs()メソッドをテストする必要があるといけない:次のように

サービスは、単にAPI呼び出しをラップします。また、サービスのテストに関するさまざまな記事をオンラインで見てきましたが、期待した結果を模倣するか、実際にWebサービスを呼び出すべきかどうかはわかりません。決して実行され得るように思わない、私はこのコードを持っているが、を期待:

import { TestBed, async, inject } from '@angular/core/testing'; 
import { HttpClientModule, HttpRequest, HttpParams } from '@angular/common/http'; 
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; 
import { LookupsService } from './Lookups.Service'; 
import { KeyValuePair } from './../../models/KeyValuePair'; 

describe(`LookupsService tests`,() => { 

    beforeEach(() => { 

    TestBed.configureTestingModule({ 
     imports: [ 
     HttpClientModule, 
     HttpClientTestingModule 
     ], 
     providers: [ 
     LookupsService 
     ] 
    }); 
    }); 



    it(`should get results from the web method`, async(inject([ LookupsService, HttpTestingController ], 

    (service: LookupsService, backend: HttpTestingController) => { 

     service.getHubs().subscribe((hubs : KeyValuePair<number>[]) => { 
     // This code never seems to run... 
     console.log(hubs.length); 
     expect(hubs.length).toBeGreaterThan(0); 
     }); 
    }))); 

}); 

完全性KeyValuePairクラスの場合は、次のようになります。

export class KeyValuePair<T> { 
    Key : string; 
    Value : T; 
} 

すべてのヘルプははるかに高く評価します!

答えて

0

ユニットテストは、実際のAPIやデータベースコールを使用せずに(少なくともほとんどの場合)あなたのコードをテストすることになっています。

これを行う1つの方法は、実際のデータの処理に使用されるライブラリ/依存関係/コードの一部を模擬することです。

FakeHttpClientサービスを作成して、偽のデータを処理し、LookupServiceサービスをテストする際に使用することができます。初期化は次のようになります。

var lookupService = new LookupService(new FakeHttpClientService()); 
//rest of your code for testing LookupService class. 

詳細はhereです。

+0

クールなので、実際のAPIに依存していないときにテストするのが最適です。だから私は実際の呼び出しを嘲笑してきました。 HttpClientを使ってサービスをテストする良い例がありますか? –

+0

いつでもメソッドを偵察するために 'spyOn'を使うことができます。使用のためにこのSOの答えを確認してください:https://stackoverflow.com/questions/30658525/spy-on-a-service-method-call-using-jasmine-spies – eminlala

関連する問題