调用karma测试时,angular未定义错误

时间:2014-05-09 22:09:53

标签: javascript angularjs karma-jasmine

我确定这与angular-mock.js有关但我无法想象我需要做什么,因为一切似乎都很好。我只是使用angular-seed应用程序的默认设置。 请帮忙摆脱问题

karma.conf.js

module.exports = function(config){
  config.set({

    basePath : '../',

    files : [
      'bower_components/angular/angular.js',
      'bower_components/angular-route/angular-route.js',
      'bower_components/angular-mocks/angular-mocks.js',
      'app/js/**/*.js',
      'test/unit/**/*.js'
    ],

    autoWatch : true,

    frameworks: ['jasmine'],

    browsers : ['Chrome'],

    plugins : [
            'karma-chrome-launcher',
            'karma-firefox-launcher',
            'karma-jasmine'
            ],

    junitReporter : {
      outputFile: 'test_out/unit.xml',
      suite: 'unit'
    }

  });
};

controllers.js

'use strict';
/* Controllers */

var app = angular.module('myApp.controllers', []);
app.constant('RESTURL', 'http://'+ location.hostname + ':8003');

app.controller('MainCtrl', ['$scope', 'dataService', 'flash', 'mySharedService','facetSearchService', 'facetSelectionService', 'RESTURL',  function($scope, dataService, flash, sharedService, facetSearch, facet, RESTURL) {
      $scope.RESTURL =  RESTURL;
      $scope.loading = true;
      $scope.data = null;
      $scope.resultCount = 0;
      $scope.currentPage = 0;
      $scope.pageSize = 10;

          ....
])}

controllerSpec.js

'use strict';

/* jasmine specs for controllers go here */

describe('controllers', function(){
  beforeEach(module('myApp.controllers'));


  it('should test MainCtrl', inject(function($controller) {
    //spec body
    var scope = {},
    ctrl = $controller('MainCtrl', {$scope:scope});

    expect($scope.RESTURL).toBe('http://'+ location.hostname + ':8003'));
  }));


});

项目文件结构: enter image description here

1 个答案:

答案 0 :(得分:14)

我相信您运行“bower install”来安装依赖项?

bower_components的路径不正确。 “.../”的基本路径将使karma查看项目的根目录,但您的bower_components位于“app”文件夹中。 karma.conf中的“files”需要以“app”为前缀。