我正在为 angular2 服务编写单元测试。代码片段:
// 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 为空。