无法读取pubsub中未定义的属性“应用”

时间:2019-11-18 10:07:10

标签: javascript angularjs unit-testing karma-jasmine

我试图在如下所示的js单元测试用例中模拟pubsub库:

在规格文件中:

beforeEach(function () {
    module('edit');

    inject(function (_$genPubsub_, _$q_) {
      $genPubsub = _$genPubsub_
      $q = _$q_;
});

在js文件中:

$ctrl.$onInit = function () { {
    $genPubsub.subscribe('edit', function(event, resources) {
        //code...
        $genPubsub.unsubscribe('edit');
      });
      $genPubsub.publish('Loaded');
}

UT失败错误:

TypeError: Cannot read property 'apply' of undefined
        at <Jasmine>
        at Object.pubsub.subscribe (src/main/webapp/resources/scripts/pubsub.js:72:32)
        at EditController.$ctrl.$onInit (src/main/webapp/resources/app/edit/editComponent.js:1:12768)
        at UserContext.<anonymous> (src/test/javascript/spec/app/edit/editComponent.spec.js:100:12)
        at <Jasmine>

到目前为止我尝试过的是:

1-我尝试通过使用provider将pubsub注入模块中:

module('edit', function($provide) {

});

我也尝试了以下解决方案:

function buildController() {
    var $ctrl = $componentController('modal', {
      $genPubsub: fakeGenPubsub
    });
    $ctrl.$onInit();
    return $ctrl;
  }

但是这两种解决方案都不适用于我,任何帮助都是可取的。

0 个答案:

没有答案