mercredi 7 septembre 2016

Unable to Test lines after .then in sinon js unit testing

Am a newborn in sinon.js, mocha, chai. Am trying to write unit-test by using above libraries. Project Front-End is in AngularJS with the combination of ECMASCRIPT6.

From the docs of Sinonjs I understand nothing! It's complicated.

Here is my JS Snippet:

login() {
    let loginData = this.loginData;
    return this.authService.login(loginData).then(userData => {
      let msg = `${this.niceToSeeYouAgain} ${userData.email}!`;
      this.userAlertsService.showSuccessToast(msg);
      this.navigationService.afterLoggedIn();
    }, errorInfo => {
      this.userAlertsService.showAlertToast(errorInfo);
    });
}

And here is my UNIT-TEST snippet:

it('.login() - should load user data and showSuccessToast and call navigationService afterLoggedIn', sinon.test(() => {

    let msg = `Nice to see you again ${userData.email}!`;

    let loginData ={
      email: "sarfaraz@walkover.in",
      password: "designer99",
      remember: true
    }

    let stub = sinon.stub(authService, 'login').resolves(userData);

    // let spy1 = sinon.spy(controller.userAlertsService, 'showSuccessToast');

    //call function
    controller.login();
    $timeout.flush();

    // expect things
    expect(stub.called).to.eq(true);


    // restore
    stub.restore();

}));

But unfortunately am stucked. My code not runs after .then lines.

am expecting it should throws error. cause I have not spies on userAlertsService and navigationService.

Please let me know what am doing wrong

Aucun commentaire:

Enregistrer un commentaire