AngularJS测试 - 服务承诺无法解决

问题描述 投票:0回答:1

我有一个返回承诺的服务。我正在尝试为它编写单元测试但有问题。我的第一次实施:

describe('Application Button Status Service', () => {
    let applicationBtnStatusService;

    beforeEach(() => {
      TestHelper().initApplicationModule();

      inject(($injector) => {
        applicationBtnStatusService = $injector.get('MwcApplicationBtnStatusService');
      });
    });

    it('CLDTX-22772: getStatusForAddDiskCloneBtn should return a promise resolving a button status', () => {
      const spy = spyOn(applicationBtnStatusService, 'getStatusForAddDiskCloneBtn').and.callThrough();

      const deferrdPromise = applicationBtnStatusService.getStatusForAddDiskCloneBtn(applicationDataMock);
      deferrdPromise.then((resolvedBtnStatus) => {
        expect(resolvedBtnStatus).toBeTruthy();
        expect(resolvedBtnStatus.btnDisabled).toBeDefined();
        expect(resolvedBtnStatus.disablingMessage).toBeDefined();
        expect(resolvedBtnStatus.btnDisabled).toBe(true);
        expect(resolvedBtnStatus.disablingMessage).toBe('Instance is running and in compatibility mode');
      });
    });
  },
);

这通过但它没有执行承诺的.then。这是我的第二个实现:

describe('Application Button Status Service', () => {
    let applicationBtnStatusService, rootScope, btnStatus;

    beforeEach(async (done) => {
      TestHelper().initApplicationModule();
      inject((
        MwcApplicationBtnStatusService: ApplicationBtnStatusService,
        $rootScope
      ) => {
        applicationBtnStatusService = MwcApplicationBtnStatusService;
        rootScope = $rootScope;
      });

      const resolvedBtnStatus = await applicationBtnStatusService.getStatusForAddDiskCloneBtn(applicationDataMock);
      rootScope.$apply();
      console.log('promise resolved', resolvedBtnStatus, '\n\n\n\n\n\n\n');
      btnStatus = resolvedBtnStatus;
      done();
    });

    it('CLDTX-22772: getStatusForAddDiskCloneBtn should return a promise resolving a button status', (done) => {
      console.log('btnStatus', btnStatus);
      expect(btnStatus).toBeDefined();
      done();
    });
  }
);

第二个失败了这个错误:

HeadlessChrome 0.0.0 (Windows 10 0.0.0) Application Button Status Service CLDTX-22772: getStatusForAddDiskCloneBtn should return a promise resolving a button status FAILED
        Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
        Expected undefined to be defined.
            at UserContext.<anonymous> (app/application/services/application-btn-status-service/application-btn-status-service.spec.js:229:27)
HeadlessChrome 0.0.0 (Windows 10 0.0.0): Executed 269 of 269 (1 FAILED) (7.443 secs / 7.383 secs)
TOTAL: 1 FAILED, 268 SUCCESS
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `npm run generate-responses && karma start karma.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Ryan.Waite\AppData\Roaming\npm-cache\_logs\2019-04-10T17_01_55_717Z-debug.log

无论我更改默认间隔,操作都需要更长的时间。

node.js angularjs typescript unit-testing karma-jasmine
1个回答
0
投票

在你的第一个解决方案中,请注入$rootScope,然后在异步调用后调用$rootScope.$digest

it('CLDTX-22772: getStatusForAddDiskCloneBtn should return a promise resolving a button status', (done) => {
      const spy = spyOn(applicationBtnStatusService, 'getStatusForAddDiskCloneBtn').and.callThrough();

      const deferrdPromise = applicationBtnStatusService.getStatusForAddDiskCloneBtn(applicationDataMock);
      deferrdPromise.then((resolvedBtnStatus) => {
        expect(resolvedBtnStatus).toBeTruthy();
        expect(resolvedBtnStatus.btnDisabled).toBeDefined();
        expect(resolvedBtnStatus.disablingMessage).toBeDefined();
        expect(resolvedBtnStatus.btnDisabled).toBe(true);
        expect(resolvedBtnStatus.disablingMessage).toBe('Instance is running and in compatibility mode');
        done();
      });
      $rootScope.$digest();
    });
© www.soinside.com 2019 - 2024. All rights reserved.