0
可以說我有一個指令:角事件偵聽器沒有出現在測試
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
之前,日誌顯示了一個函數數組,但未定義,但測試仍然失敗。
什麼導致聽衆對斷言不可見?有沒有很好的解決方法?