35
votes

I am trying to unit test an Angular service with a Jasmine spec file. This requires that a module be loaded. Here is a simple spec designed in an attempt to simply load the Angular module:

describe('Submission services', function () {
  it('Finds angular', function () {
    module('submissionServices');
  });
});

When I run Jasmine, this results in the following error

Running Jasmine specs...
FAIL: 1 test, 1 failure, 0.004 secs.

Submission services Finds angular. (/Users/XXX/Projects/globe_town/spec/javascripts/SubmissionsSpec.js:3)
ReferenceError: Can't find variable: module

Test ordering seed: --seed 1826
rake aborted!
Jasmine::Headless::TestFailure

The jasmine.yml file contains

src_files:
  - public/javascripts/jquery.js
  - spec/javascripts/lib/angular/angular.js
  - spec/javascripts/lib/angular/angular-resource.js
  - app/assets/javascripts/services/submissions.js

stylesheets:
  - stylesheets/**/*.css

helpers:
  - helpers/**/*.js

spec_files:
  - '**/*[sS]pec.js'

src_dir:

spec_dir: spec/javascripts

The versions of the software are:

  • Rails 3.2.7
  • jasmine-core 1.2.0
  • jasmine-headless-webkit 0.8.4
  • AngularJS 1.0.2

What do I need to do to make Jasmine find Angular?

2

2 Answers

71
votes

Make sure you include http://code.angularjs.org/1.1.0/angular-mocks.js for the angular-jasmine-helper functions like module.

1
votes

This is probably a remote chance but I had that same issue and found out that my angular-mocks.js file had become corrupted, there was HTML in it. I have no idea how it got there. So, if you are getting this error after trying ever fix, double check all your js files to make sure none of them are messed up or corrupted.