【发布时间】:2015-02-10 20:03:51
【问题描述】:
假设我有一个指令:
angular.module('foo').directive('myDir', function () {
return {
restrict: 'E',
link: function (scope) {
var watcher = scope.$watch('foo, function () {});
scope.$on('$destroy', function () {
watcher();
});
}
}
})
然后是下面的测试:
describe('myDir', function () {
beforeEach(function () {
module('foo');
inject(function($compile, $rootScope) {
var scope = $rootScope.$new();
$compile('<my-dir></my-dir>')(scope);
scope.$digest();
});
});
it('listens for destroy', function () {
expect(scope.$$listeners.$destroy).to.not.equal(undefined);
});
});
失败的原因是:
Error: expected undefined to not equal undefined
奇怪的是,如果我 console.log(scope.$$listeners.$destroy) 直接在我的 expect 之前,日志会显示一个函数数组,不是未定义的,但测试仍然失败。
是什么导致听众对断言不可见?有没有好的解决方法?
【问题讨论】:
标签: javascript angularjs testing karma-runner mocha.js