【问题标题】:Angular event listener not appearing in tests角度事件侦听器未出现在测试中
【发布时间】: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


    【解决方案1】:

    .to.not.equal更改

    not.to.be

    解决了问题

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 2019-12-31
      • 2021-03-09
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 2022-08-03
      相关资源
      最近更新 更多