1

sinon.js我是, mocha,的新生儿chai。我正在尝试使用上述库编写单元测试。项目前端AngularJSECMASCRIPT6.

从我的文档中Sinonjs我什么都不懂!情况很复杂。

这是我的 JS 片段:

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);
    });
}

这是我的单元测试片段:

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();

}));

但不幸的是,我被困住了。我的代码没有在行后运行.then

我期待它应该抛出错误。因为我没有监视userAlertsServiceand navigationService

请让我知道做错了什么

4

0 回答 0