Karma Jasmine testing not working
I am new to unit testing karma and I ran into this issue with 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 the following:
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 input function is not being called, and the problem is that I don't know why this is, and why it makes my test fail as the test expects the string "Hello!" to be determined.
If I remove the beforeEach that contains the injection, the test succeeds.
My goal is to make a test that verifies that $ scope.dummyFunction () is called and $ scope.settings is set. i.e:.
it('should do stuff', function () {
var ctrl = controller();
ctrl.dummyFunction();
expect(ctrl.dummyFunction).toHaveBeenCalled();
expect(ctrl.settings).toBeDefined();
});
source to share
I am new testing Node and I just solved the same problem as yours. If it helps, I solve this problem by checking all the application dependencies and putting them in the karma config file. Then I check that all dependencies ("dev" also) are installed and running karma and it works! = D
I hope this helps someone. =)
source to share