angularjs – chai-as-promise测试不适用于$q promises
我正在努力让chai-as-promised与$q承诺一起使用karma单元测试.
svc.test = function(foo){ if (!foo){ // return Promise.reject(new Error('foo is required')); return $q.reject(new Error('foo is required')); } else { // get data via ajax here return $q.resolve({}); } }; it.only('should error on no foo',function(){ var resolvedValue = MyServices.test(); $rootScope.$apply(); return resolvedValue.should.eventually.be.rejectedWith(TypeError,'foo is required'); }); 单元测试只是超时了.我不确定我在这里做错了什么来得到正确解决的承诺.使用$q似乎是一个问题 – 当我使用本机Promise.reject()时,它工作正常. 我在这里提交了一张票,但似乎没有人回应:
您需要更改测试中的执行顺序.具有chai-as-promise的异步任务需要在预期之前发生.
it('does not work',() => { $timeout.flush(); expect(myAsyncTask()).to.eventually.become('foo'); }) it('does work',() => { expect(myAsyncTask()).to.eventually.become('foo'); $timeout.flush(); }) 在刷新异步任务队列之前,需要启动对异步任务的调用. 另外,不要使用$rootScope.$digest.这可能会产生其他副作用,这些副作用在您的测试中是不可取的. $timeout.flush是你正在寻找的. https://docs.angularjs.org/api/ngMock/service/ $超时 让您的特定测试工作: it('should error on no foo',function(){ MyServices.test().should.eventually.be.rejectedWith(TypeError,'foo is required') $rootScope.$apply(); }); it('should pass on foo',function(){ MyServices.test('foo').should.eventually.become({}); $rootScope.$apply(); } TL;博士 it('async test',() => { setup(); expect(); execute(); }) it('sync test',() => { setup(); execute(); expect(); }) 鉴于发表的评论:
很公平.我认为答案是误导性的,因为没有必要进行额外的设置以使得使用Angular的承诺可以在不必处理完成的回调的情况下使用它. Fwiw,我会继续尝试撤销所说的downvote,并对此有道德.
$rootScope.$digest实际上与$rootScope相同.$apply(和$scope.$适用于此事). source $timeout.flush也将刷新非基于$timeout的函数.它不是基于$timeout的功能所独有的. Plunker展示它如何工作?: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |