TestBed.overrideProvider(不会覆盖有效的服务,ts

时间:2019-10-30 09:43:50

标签: angular unit-testing jestjs testbed ts-jest

我尝试通过BeforeEach覆盖服务,这对effect.ts无效,如果电子邮件已验证,则该条件具有条件。 但是在订阅时有效

authService.currentUser.subscribe(user => (user.emailVerified = false));

这是测试代码

class MockAuthService {
  currentUser = of(fromSeeds.userAuth);
}
describe('AuthEffects', () => {
  ...
  let authService: AuthService;

beforeEach(async () => {
   TestBed.configureTestingModule({
   ......,
  { provide: AuthService,  useClass: MockAuthService },
  ...
  })
})

 describe('listen$', () => {
    beforeEach(async () => {
       TestBed.overrideProvider(authService.currentUser, {
         useValue: of({ ...fromSeeds.userAuth, emailVerified: false })
       });
    });
    it('should', () => {
     // test here
    })
  });

还有其他信息 我使用此spyOn,它将经过验证的currentUser电子邮件更新为false,但在通过条件(如果经过验证= true)后有效。

spyOn(authService.currentUser, 'pipe').and.returnValue(
    of({ ...fromSeeds.userAuth, emailVerified: false })
);

0 个答案:

没有答案