我有这样的服务:
angular.module('domeeApp')
.factory('streamWidget', streamWidgetFactory);
function streamWidgetFactory($q) {
return {
loadContent: function() {
return $q(function(resolve, reject) {
resolve('test');
})
}
}
}
我果报/摩卡/柴测试它:
describe('streamWidget', function() {
beforeEach(module('domeeApp'));
var streamWidget;
var $timeout;
beforeEach(inject(function(_$timeout_, _streamWidget_) {
streamWidget = _streamWidget_;
$timeout = _$timeout_;
}));
it('should load new content', function(done) {
streamWidget.loadContent()
.then(function(res) {
expect(res).to.equal('test');
done();
})
.catch(function(){})
$timeout.flush();
});
});
由于$ Q承诺不符合摩卡我很喜欢this answer,这表示要添加$timeout.flush()
来强制.then
承诺的方法被执行。
的问题是,调用.flush()之后,我的所有应用被唤醒,我开始从角嘲笑得到这个错误:
Error: Unexpected request: GET /partials/page/view/index
。
我知道约$ httpBackend,但它会是疯狂嘲笑我的应用程序启动时的所有请求。
有没有办法使$q
承诺与摩卡一起工作,而无需致电$timeout.flush()
或$rootScope.$apply()
?
问题的主题是柴,而不是摩卡。可能的重复http://stackoverflow.com/a/37374041/3731501 – estus
问题的主题是$ q服务,以及我如何强制$ q promise在不使用apply/flush的情况下解决。柴没有任何关系,因为我没有使用chai-as-promise。至少,这是我看到的情况,如果你不同意,试着更好地解释它。 – pietrovismara
这是柴,断言,而不是摩卡。那么,你可以考虑使用chai-as-promised,那么$'promise可以在'then'后面调用'apply()'或'$ digest()'来完成断言。 – estus