Question

I'm starting writing some tests with Karma and for begging I just decided to try out simple test. However, I get two errors (one related with jasmine, and other with inject (I get same error when I try angular.inject):

two errors Firefox 22.0 (Windows) ConfigurationController encountered a declaration exception FAILED
    TypeError: this.func.apply is not a function in 
/adapter/lib/jasmine.js?1374202126000 (line 1145)
    testacular.js:106
    :9876/context.html:40

    ReferenceError: inject is not defined in     /var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js (line 7)
    @/var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js:7
    @/var/lib/tomcat7/webapps/lunchtime/test/controllers/configuration-controller.js:3

Firefox 22.0 (Windows): Executed 1 of 1 (1 FAILED) (0.48 secs / 0.011 secs)

I have simple controller:

app.controller("ConfigurationController", ["$scope", "$http", function($scope, $http) {
$scope.configuration = {};
}]);

And simple test:

'use strict';

describe('ConfigurationController', function() {
var scope, ctrl;
//you need to indicate your module in a test
beforeEach(angular.module('AmphinicyLunch'));
beforeEach(inject(function($rootScope) {
    scope = $rootScope.$new();
    ctrl = $controller("ConfigurationController", {$scope: scope})
}));

it("should have defined configuration", function($scope) {
    dump($scope.configuration);
    expect($scope.configuration).toEqual({});
});

});

Was it helpful?

Solution 2

The solution for this is to include this in karma.conf.js:

files: [
  'app/bower_components/angular/angular.js',
  'app/bower_components/angular-mocks/angular-mocks.js', <--------- notice mocks here
  'app/bower_components/angular-resource/angular-resource.js',
  'app/bower_components/angular-cookies/angular-cookies.js',
  'app/bower_components/angular-sanitize/angular-sanitize.js',
  'app/bower_components/angular-route/angular-route.js',
  'app/scripts/*.js',
  'app/scripts/**/*.js',
  'test/mock/**/*.js',
  'test/spec/**/*.js'
],

OTHER TIPS

For the inject error, you need to include angular-mocks.js. both module and inject are defined in that file. I'm afraid I don't know about the Jasmine error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top