我有一个返回承诺的服务。 我正在尝试为它编写一个单元测试,但遇到了问题。 我的第一个实现:
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, 'nnnnnnn');
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! cloudistics-mwc@0.0.1 test: `npm run generate-responses && karma start karma.config.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cloudistics-mwc@0.0.1 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:UsersRyan.WaiteAppDataRoamingnpm-cache_logs2019-04-10T17_01_55_717Z-debug.log
无论我更改默认间隔如何,操作都需要比它更长的时间。
在您的第一个解决方案中,请注入$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();
});