我们最近升级到 Angular 1.5-rc0。最近对 ngMocks 的一项更改破坏了$rootScope
每次测试之后的操作。但是,我很难弄清楚为什么我们在应用程序中使用的承诺在第一次之后就不会触发。
这是一个模拟右键单击事件以测试上下文菜单指令的测试。该指令使用另一个实用程序来加载 HTML 模板、缓存它等。该服务在加载模板时解析承诺。
然而,在 Angular/ngMock 1.5 中,promise 只解析第一个“it”块。$rootScope
应该重新创建每个测试,所以我不明白是什么阻碍了它。
describe('context-menu directive', function() {
beforeEach(function() {
module('templates', 'contextMenu');
inject(function(_$compile_, _$rootScope_) {
var scope = _$rootScope_.$new();
scope.choices = [{
text: 'Test',
handler: function() {}
}];
$template = _$compile_('<div context-menu="choices"></div>')(scope);
});
});
it('compiles without choices', function() {
});
it('right click triggers menu', function() {
helpers.rightClick($template);
// The event triggers a template to load and resolve a promise
// However, that promise ONLY fires if the previous "it" block is removed
$menu = $('.context-menu');
console.log($menu.length);
});
});
我尝试手动调用$rootScope.$apply()
以强制解决承诺,但这在这里没有帮助。
这是我们的模板帮助器中解决承诺的重要部分:
var deferred = $q.defer();
// Check cache
var template = $templateCache.get(templateUrl);
if (template) {
deferred.resolve(template);
}
我已经验证了它deferred.resolve
的调用,但then
听众从来没有这样做过。