【问题标题】:cannot use beforeEach with the inject function不能将 beforeEach 与注入功能一起使用
【发布时间】:2016-01-19 23:01:43
【问题描述】:

这是来自 karma.conf.js 文件:

module.exports = function (config) {
  config.set({
      frameworks: ['mocha', 'chai'],
      files: [
        'bower_components/angular/angular.js',
        'bower_components/angular-mocks/angular-mocks.js',
        'public/ng-app/module.js',
        'public/ng-app/**/*.js',
        'test/ng/**/*.spec.js'
      ],
  ...

我正在尝试像这样将 beforeEach 函数与注入一起使用:

describe('my.test', function () {
  beforeEach(module('app'));

  var MyService;
  beforeEach(inject(function (_MyService_) {
    MyService = _MyService_;
  }));

  describe('#send', function () {
    it('exists', function () {
      expect(MyService.save).to.exist;
    });
  });

});

但是 beforeEach(inject(function (...)...); 部分代码会导致此错误 当我尝试运行测试时:

PhantomJS 1.9.8 (Windows 8 0.0.0) my.test "before each" hook: workFn for "exists" FAILED
        Error: [$injector:modulerr] Failed to instantiate module ng due to:
        TypeError: 'undefined' is not an object (evaluating 'Function.prototype.bind.apply')

我不知道问题出在哪里。有人有什么想法吗?谢谢。

【问题讨论】:

    标签: angularjs karma-mocha angular-mock


    【解决方案1】:

    https://github.com/angular/angular.js/issues/13794

    bind 或 Phantom2 使用 Polyfill 或在 Chrome 中测试

    【讨论】:

    【解决方案2】:

    我从 Angular 1.5.0 降级到 1.2.29,现在测试没有错误。

    【讨论】:

      猜你喜欢
      • 2015-12-12
      • 1970-01-01
      • 2018-12-10
      • 2021-04-04
      • 2015-02-11
      • 2017-03-22
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      相关资源
      最近更新 更多