Jef*_*eff 4 boolean http mocking typescript angular
我想测试将返回true或false作为响应的端点的使用。不'true'只是 的布尔值true。我正在使用该@angular/common/http/testing模块。对于我可以使用的其他响应值TestResponse.flush(value),但这不适用于boolean值。相反,测试模块抱怨
响应类型不支持自动转换为 JSON。
这是我的测试代码:
const FLUSH_OK = {status: 200, statusText: 'Ok'};
//.. describe...
it('should work', async(() => {
service.myFunction().subscribe((data) => { // my Function returns Observable<boolean>, the real endpoint returns a true/false boolean
expect(data).toEqual(true);
});
// this fails: Failed: Automatic conversion to JSON is not supported for response type.
httpMock.expectOne((req) => {
return req.url === MY_URL;
}).flush(true, FLUSH_OK);
// this also fails: Expected 'true' to equal true.
// httpMock.expectOne((req) => {
// return req.url === MY_URL;
// }).flush('true', FLUSH_OK);
}));
Run Code Online (Sandbox Code Playgroud)
Lou*_*uis 10
使用该event方法而不是flush允许您指定响应正文的类型。
const req = httpMock.expectOne('some-url');
req.event(new HttpResponse<boolean>({body: true}));
Run Code Online (Sandbox Code Playgroud)
您还可以选择设置其他属性,例如status或statusText。