[单元测试:订阅可观察的返回未定义

问题描述 投票:-1回答:2

我在使用离子和角度的应用程序上工作,并且我遇到了单元测试返回未定义的问题,但是当运行该应用程序时,一切都很好。我的代码:

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();
});
});

karma

我希望能帮助我=(

angular typescript unit-testing jasmine karma-jasmine
2个回答
0
投票

请在问题中输入您的代码,而不是屏幕截图,以便尝试帮助的人们不必抄录您的代码...

但是将测试代码移到错误块中,以便在可观察到的代码发出后执行:

errRes => {
   expect(errRes.error.error.message).toEqual(e);
   done();
}

0
投票

我尝试了最后一个答案,但是遇到了这个问题=>

错误:超时-异步回调未在5000毫秒内调用(由jasmine.DEFAULT_TIMEOUT_INTERVAL设置)

© www.soinside.com 2019 - 2024. All rights reserved.