2017-07-14 108 views
0

我被困在一個失敗的unitest,但我不知道爲什麼。Angular 2模擬http post單元測試失敗

所以我有一個服務,我想測試。一個簡單的服務發佈到API網址。

這裏是服務

import { Injectable } from '@angular/core'; 
import { Http, Response, RequestOptions, Headers } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 
import { environment } from '../../../environments/environment'; 
import 'rxjs/add/operator/map'; 

import { ICarrierDetails } from './carrier-details'; 

@Injectable() 
export class CarrierService { 

    apiGatewayCartUri = environment.cartApiUri; 

    constructor(private http: Http) { } 

    getCarriersDetails(): Observable<ICarrierDetails[]> { 
    return this.http.post(this.apiGatewayCartUri + 'carriers/', {}) 
     .map((response: Response) => <ICarrierDetails[]>response.json()); 
    } 

} 

代碼這裏是我的規格文件:

import { CarrierService } from './carrier.service' 
import { Observable } from 'rxjs/Observable'; 
import { environment } from '../../../environments/environment'; 
import 'rxjs/add/observable/of'; 

describe('CarrierService',() => { 
    let carrierService: CarrierService; 
    let mockHttp; 
    let apiGatewayCartUri; 


    beforeEach(() => { 
     apiGatewayCartUri = environment.cartApiUri; 
     mockHttp = jasmine.createSpyObj('mockHttp', ['post']) 
     carrierService = new CarrierService(mockHttp)  
    }); 

    describe('Should call http.post method with the right URL',() => {   
     mockHttp.post.and.returnValue(Observable.of(false)); 
     carrierService.getCarriersDetails(); 
     expect(mockHttp.post).toHaveBeenCalledWith(apiGatewayCartUri + 'carriers/', {}); 
    }); 
}); 

這是例外,我讓所有的時間在控制檯:

鉻59.0.3071 (Mac OS X 10.12.5)CarrierService應該使用正確的URL調用http.post方法遇到聲明異常失敗 TypeError:無法讀取undef的屬性'post'在套房裏註冊了 。 (http://localhost:9883/_karma_webpack_/main.bundle.js:97:17) at ZoneDelegate.webpackJsonp .../../../../zone.js/dist/zone.js.ZoneDelegate.invoke(http://localhost:9883/_karma_webpack_/polyfills.bundle.js:10672:26) at Zone.webpackJsonp .../.. /。 ./../zone.js/dist/zone.js.Zone.run(http://localhost:9883/_karma_webpack_/polyfills.bundle.js:10464:43) at Suite。 (http://localhost:9883/_karma_webpack_/vendor.bundle.js:2195:29) at Env.jasmineEnv。(匿名函數)[如描述](http://localhost:9883/_karma_webpack_/vendor.bundle.js:2172:38) at Suite。 (http://localhost:9883/_karma_webpack_/main.bundle.js:96:5) at ZoneDelegate.webpackJsonp .../../../../zone.js/dist/zone.js.ZoneDelegate.invoke(http://localhost:9883/_karma_webpack_/polyfills.bundle.js:10672:26) at Zone.webpackJsonp .../.. /。 ./../zone.js/dist/zone.js.Zone.run(http://localhost:9883/_karma_webpack_/polyfills.bundle.js:10464:43) at Suite。 (http://localhost:9883/_karma_webpack_/vendor.bundle.js:2195:29

+0

我覺得這部分是錯誤的'carrierService =新CarrierService(mockHttp)',你不能在單元測試使用這樣的服務。有關服務測試的更多擴展http://chariotsolutions.com/blog/post/testing-angular-2-0-x-services-http-jasmine-karma/ – onetwo12

回答

0

我認爲你的問題是在beforeEach方法。它應該是異步功能。

嘗試使用這樣的:

import { async } from '@angular/core/testing'; 

beforeEach(async(() => { 
    apiGatewayCartUri = environment.cartApiUri; 
    mockHttp = jasmine.createSpyObj('mockHttp', ['post']); 
    carrierService = new CarrierService(mockHttp); 
})); 
+0

嗯,我得到編譯錯誤「無法找到名稱'異步'。 「我也嘗試聲明變量async「let async;」但後來我得到「TypeError:異步不是一個功能」 – Eldlabs

+0

@Eldlabs看到更新。 – onetwo12

+0

SRY沒有運氣,仍然得到問題: 鉻59.0.3071(Mac OS X的10.12.5)CarrierService應該調用http.post方法與正確的URL中遇到的聲明失敗,異常 \t類型錯誤:無法讀取屬性的「後」 undefined \t at Suite。 (http:// localhost:9884/_karma_webpack_/main.bundle.js:101:17)... – Eldlabs