unit testing - AngularJS Karma Jasmine Error: Cannot Read Property of Undefined -
this first attempt @ using karma/jasmine. want use test angularjs app.
this simple hello world test make sure things working not.
i trying test controller called inboxcontroller. have defined scope variable: $scope.greeting = 'hello world'.
i trying test in separate file called controllers-test.js.
this test:
'use strict';
describe('controllers tests ', function () {
describe('inboxcontroller', function () { var scope; beforeeach(angular.mock.module('myapp')); beforeeach(angular.mock.inject(function($rootscope, $controller){ scope = $rootscope.$new(); $controller('inboxcontroller', {$scope: scope}); })); it('should return hello world', function () { expect(scope.greeting).tobe('hello world'); }); });
});
i keep getting error:
cannot read property 'greeting' of undefined, relates scope.greeting in test code.
karma config:
files : [ 'bower_components/angular/angular.js', 'bower_components/angular-mocks/angular-mocks.js', 'bower_components/angular-resource/angular-resource.js', 'app.js', 'js/*.js', 'test/*.js' ],
inboxcontroller:
app.controller('inboxcontroller', function($rootscope, $scope, $location, inboxservice) { $scope.greeting = 'hello world'; $rootscope.loggedin = true; // load inbox $scope.loadinbox = function() { $scope.emails = inboxservice.getmessages().success(function(jsondata) { $scope.emails = jsondata; }); } // delete email $scope.delete = function (id) { inboxservice.delete(id).success(function() { $scope.loadinbox(); }); }; $scope.loadinbox();
});
solved issue myself. needed reference angular dependencies in karma config file:
files : [ 'bower_components/angular/angular.js', 'bower_components/angular-route/angular-route.js', 'bower_components/angular-sanitize/angular-sanitize.js', 'bower_components/angular-cookies/angular-cookies.js', 'bower_components/angular-bootstrap/ui-bootstrap.js', 'bower_components/angular-bootstrap/ui-bootstrap-tpls.js', 'bower_components/angular-mocks/angular-mocks.js', 'bower_components/angular-resource/angular-resource.js', 'app.js', 'js/*.js', 'test/*.js' ],
it important 1 comes first:
bower_components/angular/angular.js