Search code examples
angularjskarma-runnerrubyminekarma-jasmine

Testing AngularJS controller with Jasmine causes error in RubyMine


I try to test my AngularJS controller with Jasmine in RubyMine.

Here is my test

'use strict';

describe('MainCtrl', function(){ var scope;

beforeEach(module('myModule'));

beforeEach(inject(function($rootScope) { scope = $rootScope.$new(); }));

it('should define more than 5 awesome things', inject(function($controller) { expect(scope.awesomeThings).toBeUndefined();

$controller('MainCtrl', {
  $scope: scope
});

expect(angular.isArray(scope.awesomeThings)).toBeTruthy();
expect(scope.awesomeThings.length > 5).toBeTruthy();   })); });

I use Karma for run this test. For configure RubyMine to run tests I made all as it was written in those tutorials https://www.jetbrains.com/ruby/webhelp/preparing-to-use-karma-test-runner.html https://www.jetbrains.com/ruby/webhelp/running-unit-tests-on-karma.html

Here is my Karma config file

'use strict';

module.exports = function(config) {

config.set({ autoWatch : false,

frameworks: ['jasmine'],

browsers : ['PhantomJS'],

plugins : [
    'karma-phantomjs-launcher',
    'karma-jasmine'
]   }); };

But I got this error when I tried to run my test

src/app/main/main.controller.spec.js:3 describe('MainCtrl', function(){ ^ ReferenceError: describe is not defined

at Object.<anonymous> (src/app/main/main.controller.spec.js:3:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3

How can I fix this? I'd be glad for any help.


Solution

  • In addition to using the karma runner. You need to add something like the following to your config:

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

    with appropriate list of files. You need to include all specs as well as any files you need loaded for your app, such as angular and other libraries.