【问题标题】:Controller is undefined in jasmine test茉莉花测试中未定义控制器
【发布时间】:2016-07-28 17:26:29
【问题描述】:

karma.config.js:

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine'],
    files: [
      'node_modules/angular/angular.min.js',
      'node_modules/angular-mocks/angular-mocks.js',
      'node_modules/angular-translate/dist/angular-translate.min.js',
      'browser/javascripts/*.js',
      'browser/tests/*.spec.js'
    ],
    exclude: [],
    preprocessors: {},
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    concurrency: Infinity
  })
};

home.spec.js:

describe('Home Controller', function() {
  beforeEach(
    module('pascalprecht.translate'),
    module('tradeshiftApp')
  );
  var $controller;
  beforeEach(inject(function(_$controller_){
    $controller = _$controller_;
  }));

  it('should exist', function(){
    controller = $controller('HomeController', {
      $scope: {}
    });
    expect(controller).not.toBe(undefined);
  })
});

我正在使用karma-jasmine,问题如下:我的app.js 文件有这个模块并且它正在正确加载:

var app = angular.module('tradeshiftApp', ['pascalprecht.translate']);

但是当我尝试模拟我的控制器时,

app.controller('HomeController', function ($scope, $req, $window, $translate, $q) { 
  // some code 
});

我收到一个错误,指出HomeController 不是函数。如您所见,依赖关系已连接起来,我猜一切应该都很好。有什么建议吗?

注意:我尝试注入$rootScope并获取$rootScope.$new(),并且成功了。

【问题讨论】:

    标签: javascript angularjs jasmine karma-jasmine angular-mock


    【解决方案1】:

    试试这个:

    beforeEach(function() {
          angular.module('pascalprecht.translate', [])
          angular.mock.module('tradeshiftApp')
       }
    );

    【讨论】:

    • 它会抛出一个错误TypeError: queueableFn.fn.call is not a function
    【解决方案2】:

    所以,问题出在'pascalprecht.translate' 模块中。在我的代码中,我有一个连接到上述模块的小依赖项,该模块包含在我的 HTML 文件中,但不在我的 Karma 配置文件中。伙计们,请注意您的代码:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 2018-07-17
      • 2013-06-16
      • 1970-01-01
      相关资源
      最近更新 更多