无法在单元测试中验证JSON的结构,并且结果也不符合预期的结果

问题描述 投票:0回答:1

我正在尝试为收到unit test响应的函数调用http.post

  handleSuccessResponseForUserProfileRequest(res: HttpSentEvent|HttpHeaderResponse|HttpResponse<any>|HttpProgressEvent|HttpUserEvent<any>) {
    const ev = <HttpEvent<any>>(res);
    if (ev.type === HttpEventType.Response) {
      console.log('response from server: body ',ev.body);
      const isResponseStructureOK: boolean = this.helper.validateServerResponseStructure(ev.body);
      if (isResponseStructureOK) {
        const response: ServerResponseAPI = ev.body;
        this.userProfileSubject.next(new Result(response.result, response['additional-info']));
      } else {
        this.userProfileSubject.next(new Result('error', 'Invalid response structure from server'));
      }
    } else {
    }
  }

validateServerResponseStructure检查响应的结构是否正确。它应该具有resultadditional-info

validateServerResponseStructure(res: any): boolean {
      const keys = Object.keys(res);
      const isTypeCorrect: boolean = (
        ['result', 'additional-info'].every(key => keys.includes(key))
        && keys.length === 2);
      return isTypeCorrect;

  }

我写的单位案例是

fit('should return if user information isn\'t stored', () => {
  const body = JSON.stringify({"result":"success", "additional-info":"some additional info"});
  const receivedHttpEvent = new HttpResponse({body:body});
  const userService: UserManagementService = TestBed.get(UserManagementService);
  const helperService:HelperService = TestBed.get(HelperService);
  spyOn(userService['userProfileSubject'],'next');
  //spyOn(helperService,'validateServerResponseStructure').and.returnValue(true);
  userService.handleSuccessResponseForUserProfileRequest(receivedHttpEvent);
  const expectedResult = new Result('success', 'some additional info');
  expect(userService['userProfileSubject'].next).toHaveBeenCalledWith(expectedResult);

});

[如果我没有在spy上使用validateServerResponseStructure,那么我的测试用例将失败,因为validateServerResponseStructure失败了,尽管在我看来,该结构还可以。

Expected spy next to have been called with [ Result({ result: 'success', additionalInfo: 'some additional info' }) ] but actual calls were [ Result({ result: 'error', additionalInfo: 'Invalid response structure from server' }) ].

如果我监视validateServerResponseStructure并返回true,则会出现错误

Expected spy next to have been called with [ Result({ result: 'success', additionalInfo: 'some additional info' }) ] but actual calls were [ Result({ result: undefined, additionalInfo: undefined }) ].

有趣的是,如果我添加以下两个打印件,它们将显示不同的值!

console.log('extracted response ',response);
        console.log('sending response '+response.result + 'and additional info' +response['additional-info']);

我知道

extracted response  {"result":"success","additional-info":"some additional info"}
sending response undefined and additional info undefined

我在做什么错?

angular6 karma-jasmine angular-testing
1个回答
0
投票

有趣的是,如果我将单元测试中type中的<T> httpResponse更改为Object而不是string,则该代码有效。

const body = {"result":"success", "additional-info":"some additional info"};
      const receivedHttpEvent = new HttpResponse({body:body});
© www.soinside.com 2019 - 2024. All rights reserved.