0

我的服务中有以下方法

initialStateData() {
    if (!this.hasLoaded()) { 
        console.log('do you see me?? 1');
        // next line erroring out here on..
        console.log('do you see me?? 2');
        this._dataLoadPromise = new Promise((resolve, _reject) => {
                this.actionManager.initLoad();
                const promises = [];
                promises.push(this.getFacs());
                promises.push(this.getRoles());
                promises.push(this.getParams());
                promises.push(this.getSecurityRights());

                Promise.all(promises).then(() => {
                    this.actionManager.completeInitialLoad(this.initialState, this.initialStaticData);

                    this.fac$.subscribe((fac) => this.loadFacBasedData(fac, false));
                    this.fg$.subscribe((fg) => this.loadFacBasedData(fg, true));
                    this.rightSets$.subscribe((rightSets) => this.getUsersForSecRights(rightSets));
                    this._hasLoaded = true;
                    resolve();
                })
                .catch((err) => {
                    this.actionManager.dispatchAction('ERROR_OCCURRED', 'in initialStateData()');
                    this.HandleError(err);
                });
            });
    }
}

我的测试用例如下:

describe('initialStateData', () => {
    it('makes expected calls', () => {
      const actionManagerStub: ActionManager = TestBed.get(ActionManager);
      service.initialStaticData = { rights: [{ GroupKey: 33, GroupName: 'Manager', Description: null, GroupIsShown: true }], roles: [{ roleId: 'a0986679', roleName: 'admin' } ] } ;
      spyOn(service, 'getSecurityRights').and.callThrough();
      spyOn(actionManagerStub, 'initLoad');
      spyOn(actionManagerStub, 'completeInitialLoad').and.callThrough();
      spyOn(actionManagerStub, 'dispatchAction').and.callThrough();
      (<jasmine.Spy>service.initialStateData).and.callThrough();
      service.initialStateData();
      expect(service.getRightSetsForRoles).toHaveBeenCalled();
      expect(service.getLastCoders).toHaveBeenCalled();
      expect(service.getDischarges).toHaveBeenCalled();
      expect(service.getGrouperDescription).toHaveBeenCalled();
      expect(service.getUsersForRightSets).toHaveBeenCalled();
      expect(actionManagerStub.initLoad).toHaveBeenCalled();
      expect(actionManagerStub.completeInitialLoad).toHaveBeenCalled();
      expect(actionManagerStub.dispatchAction).toHaveBeenCalled();
    });
  });

我在控制台中看到了这一点 do you see me?? 1,但在控制台do you see me?? 2中也看到了以下错误

context.js:232 未处理的承诺拒绝:调度失败:您是否忘记配置您的商店?

如何让茉莉花从测试用例执行到 new Promise((resolve, _reject)) 块中?

任何帮助表示赞赏..

4

1 回答 1

0

这可以通过done在测试用例中使用参数来解决,并在您认为承诺已解决时调用它。前任:

it('makes expected calls', (done) => {
...settingup spys etc
service.initialStateData();
...assertions
servie._dataLoadPromise.then(() => {
...moreassertions
done();
});
};
于 2019-08-14T07:07:52.507 回答