我在使用离子和角度的应用程序上工作,并且我遇到了单元测试返回未定义的问题,但是当运行该应用程序时,一切都很好。我的代码:
auth.service.ts
login(email: string, password: string) {
return this.http
.post<AuthResponseData>(
`https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=${
environment.firebaseAPIKey
}`,
// tslint:disable-next-line: object-literal-shorthand
{ email: email, password: password }
).pipe(tap(this.setUserData.bind(this)));}
auth.service.spec.ts
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AuthService } from './auth.service';
let server: AuthService;
describe('AuthService', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [AuthService]
}));
beforeEach(() => server = TestBed.get(AuthService));
it('fail ', done =>{
const e = 'INVALID_PASSWORD';
let result;
server.login('[email protected]', '987456').subscribe(
good => {
},
errRes => {
result = errRes.error.error.message;
}
);
expect(result).toEqual(e);
done();
});
});
我希望能帮助我=(
请在问题中输入您的代码,而不是屏幕截图,以便尝试帮助的人们不必抄录您的代码...
但是将测试代码移到错误块中,以便在可观察到的代码发出后执行:
errRes => {
expect(errRes.error.error.message).toEqual(e);
done();
}
我尝试了最后一个答案,但是遇到了这个问题=>
错误:超时-异步回调未在5000毫秒内调用(由jasmine.DEFAULT_TIMEOUT_INTERVAL设置)