带有单元测试和角度的未处理的承诺拒绝

时间:2019-02-25 01:27:24

标签: angular jasmine

在使用带有承诺的茉莉花测试时,我目前遇到问题。

错误是“未处理的承诺拒绝:”,这意味着我的诺言未能正确处理FragmentMenuItem editItem; @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { Log.i(TAG,"IN ONCREATEOPTIONS"); menu.clear(); //MUST INFLATE MENU OTHERWISE WE GET NULL ERROR. inflater.inflate(R.menu.home_actionbar,menu); editItem = menu.findItem(R.id.action_edit_group); Log.i(TAG,"edititem: "+editItem.getItemId()); super.onCreateOptionsMenu(menu, inflater); } // This is called every time the Menu opens. @Override public void onPrepareOptionsMenu(Menu menu) { Log.i(TAG,"IN THE on prepare FOR FRAGMENT."); menu.findItem(R.id.action_create_group).setVisible(false); menu.findItem(R.id.action_create_group).setEnabled(false); if(owner.equals(currUser)){ menu.findItem(R.id.action_edit_group).setEnabled(true); menu.findItem(R.id.action_edit_group).setVisible(true); } else { menu.findItem(R.id.action_edit_group).setVisible(false); menu.findItem(R.id.action_edit_group).setEnabled(false); } super.onPrepareOptionsMenu(menu); }

这是我的考试:

catch()

在函数then()中,我有以下代码:

it('tests the openRepo function with changed and invalid path', (done) => {
    const OldPath = '/old';
    const NewPath = '/invalid';
    const ProjectModalBoolean = true;
    component.path = OldPath;
    component.openFolder = NewPath;
    component.projectModalLoading = ProjectModalBoolean;
    component.openRepo().then(() => {
      expect(component.openFolder).toBe('');
      expect(component.projectModalLoading).toBeFalsy();
      done();
    });
  });

...调用函数:

openRepo

return this.gitService.setPath(this.openFolder) .then((data) => { this.projectModalLoading = false; this.projectModalVisible = false; this.openFolder = ''; this.toastr.info(data.message, data.title); }) .catch((data) => { this.projectModalLoading = false; this.openFolder = ''; this.toastr.error(data.message, data.title); }); 中的async setPath(newPath) { new Promise<ServiceResult>((resolve, reject) => { if (newPath === '/new') { resolve(new Object()); } else { reject(new Object()); } }); } 似乎是个问题,因为另一项通过reject()的测试工作正常

欢迎任何帮助

1 个答案:

答案 0 :(得分:1)

您在Promise方法上有resolverejectsetPath。代替catch子句,使用第二个参数回调,如下所示。

    return this.gitService.setPath(this.openFolder)
              .then((data) => {
                this.projectModalLoading = false;
                this.projectModalVisible = false;
                this.openFolder = '';
                this.toastr.info(data.message, data.title);
              },
              (data) => {
                this.projectModalLoading = false;
                this.openFolder = '';
                this.toastr.error(data.message, data.title);
              });

阅读此article