[英]Angular event listener not appearing in tests
可以說我有一條指令:
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
奇怪的是,如果我直接在我的expect
之前console.log(scope.$$listeners.$destroy)
,則日志顯示了一組函數,不是未定義的,但是測試仍然失敗。
是什么導致聽眾對斷言不可見? 有沒有好的解決方法?
從不.to.not.equal
至
not.to.be
解決了問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.