Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine test does not see AngularJS module

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?

like image 656
oxirane Avatar asked Oct 17 '12 15:10

oxirane


People also ask

Does AngularJS use Jasmine?

Angular CLIWhenever we create files using the CLI , as well as creating the main code file it also creates a simple Jasmine spec file named the same as the main code file but ending in . spec. ts .

How to do unit testing in AngularJS?

Testing in AngularJS is achieved by using the karma framework, a framework which has been developed by Google itself. The karma framework is installed using the node package manager. The key modules which are required to be installed for basic testing are karma, karma-chrome-launcher ,karma-jasmine, and karma-cli.

Which component is used to inject and mock AngularJS service within the unit test?

AngularJS also provides the ngMock module, which provides mocking for your tests. This is used to inject and mock AngularJS services within unit tests.


2 Answers

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

like image 104
Andrew Joslin Avatar answered Sep 20 '22 03:09

Andrew Joslin


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.

like image 45
steveareeno Avatar answered Sep 21 '22 03:09

steveareeno