Angular - mock Promise method for Jasmine unit test

别来无恙 提交于 2021-02-04 17:08:25

问题


Method to test

  public onSubmit(registerData: RegisterDataModel): void {
    this.registrationService.registerWithEmailAndPassword(registerData).then((msg: string[]) =>
      this.router.navigate(['/completeSignUp']).then(() => {
        msg.forEach(singleMessage => this.notificationService.primary(singleMessage));
      }))
      .catch((msg) => msg.forEach(singleMessage => {
        this.notificationService.danger(singleMessage);
      }));
  }

I want to test if router.navigate is called in my method. Now I want to mock my service.registerWithEmailAndPasswort Promise but somehow I cannot mock it.

My Spec File

//Stubs
const routerStub: Router = jasmine.createSpyObj('Router', ['navigate']);
const registryStub: RegistrationService = jasmine.createSpyObj('RegistrationService', ['registerWithEmailAndPassword']);

Unit test

  it('should navigate on promise - success', () => {
    (<jasmine.Spy>registryStub.registerWithEmailAndPassword).and.callThrough();
    const spy = (<jasmine.Spy>routerStub.navigate);
    component.onSubmit({username: null, email: null, password: null, passwordConfirm: null, termsAndCondition: null});
    expect(spy).toHaveBeenCalledWith(['/completeSignUp']);
  });

The Error that is appearing is: TypeError: Cannot read property 'then' of undefined Does anyone how to proper mock this service?

Edit

I have also tried to mock the promise like:

    (<jasmine.Spy>registryStub.registerWithEmailAndPassword)
  .and.returnValue(new Promise(() => Promise.resolve()));

But it still throws me:

Expected spy Router.navigate to have been called with [ [ '/completeSignUp' ] ] but it was never called.

回答1:


As silicon Soul mentioned you need definately mock the router.navigate promise with a returnvalue as otherwise it will ent into a Promise.reject(). By adding (<jasmine.Spy>routerStub.navigate).and.returnValue(Promise.resolve()); the unit test should be ok. The final unit test should look like:

  it('should navigate on promise - success', fakeAsync(() => {
    const spy = (<jasmine.Spy>routerStub.navigate).and.returnValue(Promise.resolve());
    (<jasmine.Spy>registryStub.registerWithEmailAndPassword).and.returnValue(Promise.resolve(['test']));
    component.onSubmit({username: 'test', email: 'test', password: 'test', passwordConfirm: 'test', termsAndCondition: true});

    tick();
    expect(spy).toHaveBeenCalledWith(['/completeSignUp']);
  }));



回答2:


You will get the error because the registerWithEmailAndPassword spy does not return a Promise. You could use callFake to return a Promise:

(<jasmine.Spy>registryStub.registerWithEmailAndPassword).and.callFake(() => Promise.resolve([]));

Also, promises are asynchronous so you should probably use fakeAsync test and tick, or timeout or return an object with then method instead of a Promise.



来源:https://stackoverflow.com/questions/51560358/angular-mock-promise-method-for-jasmine-unit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!