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:
What do I need to do to make Jasmine find Angular?
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 .
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.
AngularJS also provides the ngMock module, which provides mocking for your tests. This is used to inject and mock AngularJS services within unit tests.
Make sure you include http://code.angularjs.org/1.1.0/angular-mocks.js for the angular-jasmine-helper functions like module
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With