jeudi 8 septembre 2016

how to handle sinon.stub().throws() in unit test by Sinon JS

Am trying to invoke fail condition in my snippet. But when I use sinon.stub().throws() method It shows me error. Am unable to handle it in code. Here is my 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();

      //above lines are covered in test cases

    }, errorInfo => {
      // below line are needed to test
      this.userAlertsService.showAlertToast(errorInfo);
    });
}

**And here is my unit-test snippet: **

it('.login() - should throw exception - in failure case', sinon.test(() => {

    let errorInfo = "some error";

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

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


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

    // expect things
    console.log(stub.callCount, stub.args[0]);

  }));

Please let me know what am doing wrong

Aucun commentaire:

Enregistrer un commentaire