title: Angular - mock Promise method for Jasmine unit test
body: <h2>Method to test</h2>
  
  <pre><code>  public onSubmit(registerData: RegisterDataModel): void {
      this.registrationService.registerWithEmailAndPassword(registerData).then((msg: string[]) =&gt;
        this.router.navigate(['/completeSignUp']).then(() =&gt; {
          msg.forEach(singleMessage =&gt; this.notificationService.primary(singleMessage));
        }))
        .catch((msg) =&gt; msg.forEach(singleMessage =&gt; {
          this.notificationService.danger(singleMessage);
        }));
    }
  </code></pre>
  
  <p>I want to test if <code>router.navigate</code> is called in my method. Now I want to mock my  <code>service.registerWithEmailAndPasswort</code> Promise but somehow I cannot mock it.</p>
  
  <h2>My Spec File</h2>
  
  <pre><code>//Stubs
  const routerStub: Router = jasmine.createSpyObj('Router', ['navigate']);
  const registryStub: RegistrationService = jasmine.createSpyObj('RegistrationService', ['registerWithEmailAndPassword']);
  </code></pre>
  
  <h1>Unit test</h1>
  
  <pre><code>  it('should navigate on promise - success', () =&gt; {
      (&lt;jasmine.Spy&gt;registryStub.registerWithEmailAndPassword).and.callThrough();
      const spy = (&lt;jasmine.Spy&gt;routerStub.navigate);
      component.onSubmit({username: null, email: null, password: null, passwordConfirm: null, termsAndCondition: null});
      expect(spy).toHaveBeenCalledWith(['/completeSignUp']);
    });
  </code></pre>
  
  <p>The Error that is appearing is: <code>TypeError: Cannot read property 'then' of undefined</code>
  Does anyone how to proper mock this service?</p>
  
  <h2>Edit</h2>
  
  <p>I have also tried to mock the promise like:</p>
  
  <pre><code>    (&lt;jasmine.Spy&gt;registryStub.registerWithEmailAndPassword)
    .and.returnValue(new Promise(() =&gt; Promise.resolve()));
  </code></pre>
  
  <p>But it still throws me:</p>
  
  <pre><code>Expected spy Router.navigate to have been called with [ [ '/completeSignUp' ] ] but it was never called.
  </code></pre>