javascript 调用业力测试时,角度未定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23574706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:07:52  来源:igfitidea点击:

angular is not defined error when karma test invoked

javascriptangularjskarma-jasmine

提问by Sudhakar

I'm sure this has something to do with angular-mock.js but I cant figure what I need to do as everything seems to be fine. I'm just going by the default setup from angular-seed app. Please help get rid of the problem

我确定这与 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

控制器.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

控制器规范.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'));
  }));


});

Project file structure: enter image description here

项目文件结构: 在此处输入图片说明

回答by jdewit

I trust you ran "bower install" to install the dependencies?

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

The paths to bower_components are incorrect. A basepath of "../" will make karma look in the root of your project but your bower_components are in your "app" folder. The "files" in karma.conf need to be prefixed with "app".

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