0

组件功能:

onSubmit() {
    this.formDataService.sendUserCredentials(this.loginForm.get('email').value, 
    this.loginForm.get('password').value)
    .subscribe(res => {
        localStorage.setItem('X-Auth-Token', res.headers.get('X-Auth-Token'));
        localStorage.setItem('Authorization', res.headers.get('Authorization'));
        localStorage.setItem('userObject', JSON.stringify(res.body));
        this.router.navigate(['dashboard']);
    }, err => {
      this.invalidCredentials = true;
    });
}

服务方式:

sendUserCredentials(userEmail, userPassword) {
    const userCredentials = {
        email: userEmail,
        password: (jsSha.sha512(userPassword)).toUpperCase(),
    };

    return this.http.post(environment.BASE_URL + 'users/login', userCredentials, {
        observe: 'response',
        headers: new HttpHeaders().set('Authorization', 'dfsdf-e4vfd-ertrg')
    });
}

单元测试:

it('API call inside onSubmit resolves successfully', fakeAsync(() => {
    const spy = spyOn(service, 'sendUserCredentials').and.returnValue(of('logged-in'));
    component.onSubmit();
    tick();
    expect(spy.calls.any()).toBe(true);
}));

我能够通过名为 as 的服务方法的行,sendUserCredentials但在该服务方法内部有一行我从响应中获取标头,但我无法解决我遇到的问题。

Error: TypeError: Cannot read property 'get' of undefined

此错误专门发生在组件函数的第 4 行。

4

0 回答 0