gpt4 book ai didi

javascript - Angular 事件监听器未出现在测试中

转载 作者:行者123 更新时间:2023-11-28 21:29:28 25 4
gpt4 key购买 nike

假设我有一个指令:

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);
});
});

失败的是:错误:预期未定义不等于未定义

奇怪的是,如果我直接在 expect 之前 console.log(scope.$$listeners.$destroy),日志会显示一个函数数组,不是未定义的,而是测试仍然失败。

是什么导致监听器对断言不可见?有好的解决方法吗?

最佳答案

.to.not.equal 更改

not.to.be

解决了问题

关于javascript - Angular 事件监听器未出现在测试中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com