Reputation: 81
I am trying to do some basic Unit testing for AngularJS using Karma. All the tests I have written seem syntactically correct. But I am having a problem at the most basic step, i.e the beforeEach part of the code. When I try running the test, I get the following problem
TypeError: Cannot read property '$injector' of null
at Object.workFn (http://localhost:9876/absolute/Users/vesriram/Documents/AngularJS%20project/vendor/js/angular-mocks.js:1698:15)
at Object.<anonymous> (http://localhost:9876/adapter/lib/angular-scenario.js:26360:54)
at Array.forEach (native)
at Object.forEach (http://localhost:9876/adapter/lib/angular-scenario.js:9593:11)
I have been trying to solve this problem for the past 36 hours and have had no luck so far. As far as I can see, noone else seems to be having this problem. This makes me think that I am possibly doing something stupid. I would really appreciate any help you people could give me. I will be happy to post any additional code that you need (so long as I am at liberty to divulge it).
The relevant code is the following-
app.js
var sell_page = angular.module("sell_page", ['ui.bootstrap']);
sell_page.controller('ItemTitleController', ['$scope','listingInformationService', '$location',function($scope, listingInformationService, $location) {
$scope.itemNames = getAllItemNames();
$scope.draftItems = getAllSavedDrafts();
document.getElementById("categorySelection").style.visibility = "hidden";
------bunch of code-------
}]);
controllersSpec.js
describe("Unit: Testing Controllers", function() {
beforeEach(angular.mock.module('sell_page'));
it('should have a ItemTitleController controller', function() {
expect(sell_page.ItemTitleController).not.to.equal(null);
});
describe("ItemTitleController", function() {
var scope;
beforeEach(angular.mock.module('sell_page'));
beforeEach(angular.mock.inject(function($rootScope, listingInformationService, $location, $controller) {
var scope = $rootScope.$new();
var controller = $controller('ItemTitleController', {
$scope : scope
});
}));
it("should display xxx properly", function() {
--some code---
});
});
karma.conf.js
basePath = '';
files = [
JASMINE,
JASMINE_ADAPTER,
'../vendor/js/angular.min.js',
'../vendor/js/angular.js',
'../vendor/js/angular-mocks.js',
'../vendor/js/angular-scenario.js',
ANGULAR_SCENARIO,
ANGULAR_SCENARIO_ADAPTER,
'../app/js/*.js',
'e2e/*.js',
'midway/*.js',
'unit/*.js',
];
exclude = [
];
reporters = ['progress'];
port = 9876;
runnerPort = 9100;
colors = true;
logLevel = LOG_INFO;
autoWatch = true;
browsers = ['Chrome'];
captureTimeout = 60000;
singleRun = false;
Upvotes: 8
Views: 5694
Reputation: 1
I have the same issue.
In my case, this error occurs when you use angular 1.0.8 and angular-mocks 1.0.8 with the newer version of Jasmine (2.0.0+, not so new actually)
angular-mock assumes some internal variable in Jasmine and it doesn't exist anymore in the new version.For more detail, see this one https://github.com/angular/angular.js/issues/5632.
The solution is either upgrade your Angular (not possible in my case) or downgrade your Jasmine.
If you use Grunt to run karma, downgrade your karma-jasmine to version '~0.1.0' will fix this issue. This version includes jasmine, so you should also remove 'jasmine-core' from your devDependencies. The set of devDependencies below worked for me.
"devDependencies": {
"grunt-karma": "^0.10.1",
"karma": "^0.12.31",
"karma-chrome-launcher": "^0.1.7",
"karma-coverage": "^0.2.7",
"karma-jasmine": "~0.1.0",
"karma-junit-reporter": "^0.2.2",
"karma-phantomjs-launcher": "^0.1.4",
"phantomjs": "^1.9.15",
}
Upvotes: 0
Reputation: 2045
I honestly don't know if this is the fix at all, and it probably only works for Mocha, but I was running into the same thing and I changed the first beforeEach bit to this:
beforeEach(function() { module('myApp'); });
and it seems to be working better. It seems like the timing of the angular.mocks.module call works out better if you let the testing framework (mocha in my case) have a wrapped version of the method.
Upvotes: 2
Reputation: 393
I commonly run into this error when the issue is simply an un-closed )
or }
Outside of that, an approach to debug the issue might be to simplify until the problem goes away. That is, start removing large portions of code, a bit at a time, until you've "binary-searched" for the issue.
Upvotes: 0