【发布时间】:2026-01-26 10:05:02
【问题描述】:
我有这个登录功能,我想测试一下。但是我收到一个错误“异步回调未在 5000 毫秒内调用”
public async Login(email: string, password: string): Promise<any> {
const body = { email, password };
await this.getCSRFToken().toPromise();
return this.http
.post<any>(this.baseUrl + 'login', body)
.pipe(
tap(data => {
this.user = data;
return this.user;
})
)
.toPromise();
}
我的测试:
it('should login', (done) => {
const service: AuthenticationService = TestBed.get(AuthenticationService);
const http = TestBed.get(HttpTestingController);
let userResponse;
service.Login('email', 'password').then((response) => {
userResponse = response;
});
http.expectOne((req) => {
return req.method === 'POST'
&& req.url === '/frontend/login';
}).flush({user_type: 'Test'});
expect(userResponse).toEqual({user_type: 'Test'});
});
有什么想法吗??
【问题讨论】:
标签: angular unit-testing jasmine angular-test