对象文字只能指定已知属性,并且“属性名称”不存在

时间:2018-11-30 12:14:21

标签: angular5 karma-jasmine

我执行服务测试时遇到错误,该测试返回angular(1.6.3)中的JSON响应。

../ my-request.component.spec.ts(30,7)中的错误:错误TS2345:类型'{ALL:string;保留:字符串; IN_PROCESS:字串; NEED_INFO:字符串;已完成:字符串; }”不能分配给“ Expected>”类型的参数。

对象文字只能指定已知的属性,并且类型'Expected>'中不存在'ALL'。

下面是我的测试规格

it('#myrequest  should call endpoint and return it\'s result', (done) => {
    backend.connections.subscribe((connection: MockConnection) => {
        expect(connection.request.method).toEqual(RequestMethod.Get);
        expect(connection.request.url).toEqual('http://localhost:3000/myrequests');
        let options = new ResponseOptions({
            body: JSON.stringify({
                ALL: "10",
                RESERVED: "2",
                IN_PROCESS: "2",
                NEED_INFO: "2",
                COMPLETED: "2"
            })
        });
        connection.mockRespond(new Response(options));
    });

    service
        .getMyRequests()
        .subscribe((response) => {
            expect(response).toEqual({
                "ALL": "10",
                "RESERVED": "2",
                "IN_PROCESS": "2",
                "NEED_INFO": "2",
                "COMPLETED": "2"
            });
            done();
        });

});

service.ts

getMyRequests(): Observable<any> {
    return this.http.get(this._url)
        .map(response => response.json());
}

来自API的响应

{
                "ALL": "10",
                "RESERVED": "2",
                "IN_PROCESS": "2",
                "NEED_INFO": "2",
                "COMPLETED": "2"
            }

0 个答案:

没有答案