登录POST请求测试出错

时间:2020-02-12 13:53:27

标签: angular typescript unit-testing express karma-jasmine

我的后端是express.js,它接受用户名和密码作为登录名。该请求应为POST,请求URL应为localhost:3000/api/login,并且响应采用问题末尾定义的格式。我正在尝试测试此行为,因此编写了以下单元测试:

it('login should be POST, have proper URL and return proper JSON', () => {
     const authService = TestBed.get(AuthenticationService);
     const http = TestBed.get(HttpTestingController);
     let loginResponse;
     const responseForm = '<form />';

     authService.login('Test', 'testtest').subscribe((response) => {
       loginResponse = response;
     });

     http.expectOne((request: HttpRequest<any>) => {
      return request.method == 'POST'
        && request.url == 'http://localhost:3000/api/login'
        && JSON.stringify(request.body) === JSON.stringify({
          _id: 'idhere', firstname: 'namehere', password: 'passwordhashhere',
        })
        && request.headers.get('Content-Type') === 'application/json';
    }).flush(responseForm);
     expect(loginResponse).toEqual(responseForm);
  });

我得到的错误是:

Error: Expected one matching request for criteria "Match by function: ", found none.

更新

我对API的回应如下: _id:'idhere', firstname: 'firstnamehere', password:'passwordhashhere'

0 个答案:

没有答案