23

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.

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

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

2 Answers 2

33

Besides installing angular-mocks through bower, remember to add reference to angular-mocks.js in your karma config file, like below

config.set({

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

    files: [
      'bower_components/angular/angular.js',
      'bower_components/angular-mocks/angular-mocks.js',
      ...
    ]
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer, however even after adding the angular-mock.js file, still Im getting the same error. I have updated my question with a screenshot for clarity
Finally got it working @rebornix, thanks for the help. I had to reinstall jasmine, karma and angular-mocks , and this link also helped ericnish.io/blog/set-up-jasmine-and-karma-for-angularjs
11

In my case it was also about wrong order of files path in 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? ;-)

1 Comment

I had the same problem, this is the solution for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.