2016-09-14 2 views
2

Http要求を行うサービスのユニットテストを作成しようとしています。MockBackendを使用して関数をテストしてから.mapを呼び出す

.map()の後にHttp.get()要求を返すサービスがあります。 .map()でエラーにならないものを返すために私の虚偽のバックエンドを取得するのに問題があります。私が得ているエラーは次のとおりです。

this._http.get(...).map is not a function 

私は全体を通して私の主なガイドとしてthis articleを使用しています。

サービス機能から.map()を削除しても、エラーは発生しません。私の呼びかけた応答を、私が呼ぶことができる.map()の機能を持たせるにはどうすればいいですか?

注:私は現在、ここRC.4

を使用していますが、私のサービスです。

import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 

import { AppSettings } from '../../../settings'; 
import { Brand } from '../../models/index'; 

@Injectable() 
export class BrandDataService { 

    allBrands : Brand[]; 
    groups : any; 
    groupNames : string[]; 

    constructor (
    private _http : Http 
) {} 

    /** 
    * Get all brands 
    */ 
    public getAllBrands() :Observable<any> { 

    let url = AppSettings.BRAND_API_URL + 'brands'; 
    return this._http.get(url) 
    .map(this.createAndCacheBrands) 
    .catch((error) => { 
     return Observable.throw(error); 
    });   
    } 

    private createAndCacheBrands (res:Response) { 
    ... 
    } 

} 

そしてここでは、バックエンドを模擬するためにMockBackendおよびその他の関連ライブラリを使用している私のspecファイルであり、これらのテストのために:

// vendor dependencies 
import { Http, BaseRequestOptions, Response, ResponseOptions, RequestMethod } from '@angular/http'; 
import { addProviders, inject } from '@angular/core/testing'; 
import { MockBackend, MockConnection } from '@angular/http/testing'; 

// Service to test 
import { BrandDataService } from './brandData.service'; 


describe('Brand data service',() => { 

    let service : BrandDataService = null; 
    let backend : MockBackend = null; 

    // Provide a mock backend implementation 
    beforeEach(() => { 
    addProviders([ 
     MockBackend, 
     BaseRequestOptions, 
     { 
     provide : Http, 
     useFactory : (backendInstance : MockBackend, defaultOptions : BaseRequestOptions) => { 
      return new Http(backendInstance, defaultOptions); 
     }, 
     deps : [MockBackend, BaseRequestOptions] 
     }, 
     BrandDataService 
    ]) 
    }) 

    beforeEach (inject([BrandDataService, MockBackend], (_service : BrandDataService, mockBackend : MockBackend) => { 
    service = _service; 
    backend = mockBackend; 
    })); 

    it ('should return all brands as an Observable<Response> when asked', (done) => { 
    // Set the mock backend to respond with the following options: 
backend.connections.subscribe((connection : MockConnection) => { 
    // Make some expectations on the request 
    expect(connection.request.method).toEqual(RequestMethod.Get); 
    // Decide what to return 
    let options = new ResponseOptions({ 
     body : JSON.stringify({ 
     success : true 
     }) 
    }); 
    connection.mockRespond(new Response(options)); 
    }); 

    // Run the test. 
    service 
    .getAllBrands() 
    .subscribe(
    (data) => { 
     expect(data).toBeDefined(); 
     done(); 
    } 
) 
    }); 
}); 

答えて

2

あなたはを使用できるようにrxjsをインポートする必要があります:

import 'rxjs/Rx'; 

それとも、あなたが使用することはありませんので、あなたのアプリがファイルをロードしないだけmapオペレーターをインポートすることができます。

import 'rxjs/add/operator/map'; 
+0

ああ、私はとても迷惑だ - 私は、以前の今日、この問題を持っていました他の場所でそれを修正しました。なぜ私には起こっていないのか分かりません。明白なことを指摘していただきありがとうございます! – dafyddPrys

+0

@dafyddPrys心配しないでください、誰にも起こります。私はこの正確な間違いを2〜3回しました。それが私がそれを思い出した方法です。 :-) –

+0

'.map()。catch()'を動かすために私がインポートする必要があることを知っていますか? - EDIT:それは 'rxjs/add/operator/catch'になります。 – dafyddPrys

関連する問題