Javascript ReferenceError:找不到变量:角度测试中的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31827257/
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-08-23 07:14:08  来源:igfitidea点击:

ReferenceError: Can't find variable: module in angular testing

javascriptangularjstestingionic-frameworkkarma-jasmine

提问by sameera207

I'm trying to write a test for my Angular controller, I'm using jasminekarmaand angular-mocks, but keeps on getting the error ReferenceError: Can't find variable: module.

我正在尝试为我的 Angular 控制器编写一个测试,我正在使用jasminekarmaand angular-mocks,但一直收到错误ReferenceError: Can't find variable: module

I had a bit of a search, but I already have the angular-mocksin my bower.

我进行了一些搜索,但angular-mocks我的凉亭里已经有了。

What could I be missing here?

我会在这里错过什么?

The following is my code:

以下是我的代码:

#controller
angular.module('cook_book_ctrl', [])
.controller('cookBookCtrl', function($scope, CookBook, CookBookRecipesService){

  $scope.cookbookoptions = true;

  CookBook.list()
   .success(function(data){
     $scope.recipeList = data;
     CookBookRecipesService.loadCookBookRecipes($scope.recipeList);
   })
   .error(function(error){
   })
  });

#controller test
describe('CookBook controller spec', function(){
  var $httpBackend, $rootScope, createController, authRequestHandler

  beforeEach(module('cook_book_ctrl'));
})

#bower.json
{
  "name": "HelloIonic",
  "private": "true",
  "devDependencies": {
    "ionic": "driftyco/ionic-bower#1.0.0",
    "ionic-service-analytics": "master",
    "ionic-service-core": "~0.1.4",
    "angular-mocks": "1.3.13"
  },
  "dependencies": {
    "ng-cordova-oauth": "~0.1.2",
    "ng-tags-input": "~2.3.0",
    "angular": "~1.4.0",
    "underscore": "~1.8.3",
    "materialize": "~0.97.0"
  },
  "resolutions": {
    "angular": "~1.4.0"
  }
}


   beforeEach(module('cook_book_ctrl'));
})

UPDATE: Screenshot added for clarity

更新:为清晰起见添加了屏幕截图

enter image description here

在此处输入图片说明

回答by Rebornix

Besides installing angular-mocksthrough bower, remember to add reference to angular-mocks.jsin your karma config file, like below

除了angular-mocks通过 bower安装,记得angular-mocks.js在你的 karma 配置文件中添加引用,如下所示

config.set({

    basePath: '../',
    port: '8000',

    files: [
      'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.js',
      ...
    ]

回答by wit0ld

In my case it was also about wrong order of files path in karma.conf.js.

就我而言,这也是关于 karma.conf.js 中文件路径的错误顺序。

Was:

曾是:

// list of files / patterns to load in the browser
files: [
  'tests/*.test.js', // this should not be as first!
  'bower_components/angular/angular.min.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'app/*.js',

],

Should be:

应该:

// list of files / patterns to load in the browser
files: [
  'bower_components/angular/angular.min.js',
  'bower_components/angular-mocks/angular-mocks.js',
  'app/*.js',
  'tests/*.test.js' // now it's cool
],

Maybe obvious thing or maybe not? ;-)

也许是显而易见的事情,也许不是?;-)