【发布时间】:2016-02-16 15:19:24
【问题描述】:
我正在为 angular2 服务编写单元测试。代码sn-ps:
// jasmine specfile
// already injected MockConnection into Http
backend.connections.subscribe ((c: MockConnection) => {
connection = c;
});
// doing get-request
myServiceCallwithHttpRequest ().subscribe (result => {
// this test passes!
expect (result).toEqual ({
"message": "No Such Object"
});
// this test fails, don't know how to get the response code
expect (whereIsResponseStatus).toBe (404);
});
connection.mockRespond (new Response (new ResponseOptions ({
body: {
"message": "No Such Object"
},
status: 404
})));
我的服务:
// service
myServiceCallwithHttpRequest (): Observable<Response> {
return this.http.get ('/my/json-service').map (res => {
// res.status == null
return res.json ()
})
.catch (this.handleError); // from angular2 tutorial
}
第一个期望没问题,程序进入 map 调用,而不是 catch。但是如何获得状态码 404? res.status 为空。
【问题讨论】:
标签: angular unit-testing typescript angular2-http