Reputation: 1278
I am running some unit tests based off of this website: http://www.bradoncode.com/blog/2015/05/19/karma-angularjs-testing/
I've wrote an app.js file and a MathController.js file.
Enclosed is the relevant code:
app.js
(function(){
var app = angular.module('app', ['ui-router', 'ng-resource', 'ui-bootstrap']);
// app.config stuff here
}());
MathController.js
(function(){
angular.module('app').controller('MathController', ['$scope', function ($scope){
$scope.sum = function(){
$scope.z = $scope.x + $scope.y;
};
}]);
}());
And here is the test spec file:
MathController.spec.js
describe('MathController', function(){
beforeEach(angular.mock.inject('app'));
var $controller;
beforeEach(angular.mock.inject(function(_$controller_){
$controller = _$controller_;
}));
it('should equal 3', function(){
var $scope = {};
var controller = $controller('MathController', {$scope: $scope });
$scope.x = 1;
$scope.y = 2;
$scope.sum();
expect($scope.z).toBe(3);
});
});
And this is the error I get in the console:
PhantomJS 2.1.1 (Linux 0.0.0) MathController should equal 3 FAILED
Error: [ng:areq] http://errors.angularjs.org/1.4.4/ng/areq?p0=fn&p1=not%20a%20function%2C%20got%20string (line 22)
.....some extra stuff omitted
workFn@/root/lib/angular/angular-mocks.js:2401:26
undefined
Error: [ng:areq] http://errors.angularjs.org/1.4.4/ng/areq?p0=MathController&p1=not%20a%20function%2C%20got%20undefined in /home/root/lib/angular/angular.min.js (line 22)
.....some extra stuff omitted
/home/root/spec/MathController.spec.js:13:31
PhantomJS 2.1.1 (Linux 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.041 secs / 0.006 secs)
So it says MathController is not a function, and it says angular picked up a string when it should have picked up a function. Can anyone point me in the right direction of how to fix this?
Upvotes: 1
Views: 1475
Reputation: 1122
I think you need this?
describe('MathController', function(){
beforeEach(angular.mock.module('app'));
var $controller;
beforeEach(angular.mock.inject(function(_$controller_){
$controller = _$controller_;
}));
it('should equal 3', function(){
var $scope = {};
var controller = $controller('MathController', {$scope: $scope });
$scope.x = 1;
$scope.y = 2;
$scope.sum();
expect($scope.z).toBe(3);
});
});
The difference being: angular.mock.module('app')
Upvotes: 1