2017-07-14 110 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