Reputation: 61
I am new to karma unit testing and I have encountered this problem to a simple Ionic Application.
This is my controller:
angular.module('starter').controller('AccountCtrl', function ($scope) {
$scope.settings = {
enableFriends: true
};
$scope.dummyFunction = function () {
console.log("Just do nothing!");
}
});
This is my unit testing file:
describe('AccountCtrl', function () {
var scope, createController;
beforeEach(module('starter'));
console.log("0");
beforeEach(function () {
console.log("1");
})
beforeEach(inject(function ($rootScope, $controller) {
console.log("2");
scope = $rootScope.$new();
controller = $controller('AccountCtrl', {
'$scope': scope
});
}));
it('should do stuff', function () {
console.log("3");
expect("Hello!").toBeDefined();
});
});
At this point, when I run the test I get this:
PhantomJS 2.1.1 (Mac OS X 0.0.0) LOG: 'WARNING: Tried to load angular more than once.'
PhantomJS 2.1.1 (Mac OS X 0.0.0) LOG: '0'
LOG: '1'
LOG: '3'
PhantomJS 2.1.1 (Mac OS X 0.0.0) AccountCtrl should do stuff FAILED
forEach@/Users/eduard.lache/Documents/dummyProjects/unitTestingAPP/www/lib/ionic/js/ionic.bundle.js:13696:24
loadModules@/Users/eduard.lache/Documents/dummyProjects/unitTestingAPP/www/lib/ionic/js/ionic.bundle.js:17883:12
createInjector@/Users/eduard.lache/Documents/dummyProjects/unitTestingAPP/www/lib/ionic/js/ionic.bundle.js:17805:30
workFn@/Users/eduard.lache/Documents/dummyProjects/unitTestingAPP/www/lib/angular-mocks/angular-mocks.js:2353:60
/Users/eduard.lache/Documents/dummyProjects/unitTestingAPP/www/lib/ionic/js/ionic.bundle.js:17923:53
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.004 secs / 0.011 secs)
As you can see from the console.log messages, the inject function is not getting called and the problem is that I don't know why is that and why is this making my test fail as the test expects the string "Hello!" to be defined, which it is.
If I remove the beforeEach that contains the injection the test is successfull.
My goal is to make a test that verifies that $scope.dummyFunction() has been called and that $scope.settings is defined. i.e.:
it('should do stuff', function () {
var ctrl = controller();
ctrl.dummyFunction();
expect(ctrl.dummyFunction).toHaveBeenCalled();
expect(ctrl.settings).toBeDefined();
});
Upvotes: 3
Views: 1194
Reputation: 781
I'm new testing Node and I just solved the same problem that you had. If it can you help, I solve this problem check all dependencies of app and put they in karma's configuration file. Then, I check that all dependencies ("dev" also) are installed and run karma, and this works! =D
I hope it helps someone. =)
Upvotes: 0