简体   繁体   English

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

[英]ReferenceError: Can't find variable: module in angular testing

I'm trying to write a test for my Angular controller, I'm using jasmine karma and angular-mocks , but keeps on getting the error ReferenceError: Can't find variable: module . 我正在尝试为我的Angular控制器编写一个测试,我正在使用jasmine karmaangular-mocks ,但继续得到错误ReferenceError: Can't find variable: module

I had a bit of a search , but I already have the angular-mocks in 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 更新:为清晰起见添加了屏幕截图

在此输入图像描述

Besides installing angular-mocks through bower, remember to add reference to angular-mocks.js in your karma config file, like below 除了通过凉亭安装angular-mocks angular-mocks.js ,记得在你的karma配置文件中添加对angular-mocks.js引用,如下所示

config.set({

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

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

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? 也许是显而易见的事情,也 ;-) ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM