У меня есть служба, которая возвращает обещание.я пытаюсь написать модульный тест для него, но возникают проблемы.моя первая реализация:
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! 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:\Users\Ryan.Waite\AppData\Roaming\npm-cache\_logs\2019-04-10T17_01_55_717Z-debug.log
и независимо от того, что я изменяю интервал по умолчанию, операции занимают больше времени, чем он.