javascript - Karma + Angular undefined error -
i started angular testing karma , jasmine. i've written 2 basic tests. 1 of 2 tests pass, other 1 failing. can't seem debug it. i've been looking everywhere, , should work according various tutorials. $scope variable should available. following error (and lot of text, see ):
@ c:/totalflatline-2.0/public/lib/angular/angular.js:4362 @ foreach (c:/totalflatline-2.0/public/lib/angular/angular.js:336) @ loadmodules (c:/totalflatline-2.0/public/lib/angular/angular.js:4364) @ createinjector (c:/totalflatline-2.0/public/lib/angular/angular.js:4248) @ workfn (c:/totalflatline-2.0/public/lib/angular-mocks/angular-mocks.js:2409) @ c:/totalflatline-2.0/public/lib/angular-mocks/angular-mocks.js:2392 @ c:/totalflatline-2.0/public/shared/tests/unit/shared.client.controller.unit.tests.js: 5 @ c:/totalflatline-2.0/node_modules/karma-jasmine/lib/boot.js:126 @ c:/totalflatline-2.0/node_modules/karma-jasmine/lib/adapter.js:171 @ http://localhost:9876/karma.js:210 @ http://localhost:9876/context.html:83 typeerror: 'undefined' not object (evaluating '$scope.test') @ c:/totalflatline-2.0/public/shared/tests/unit/shared.client.controller.unit.tests.js: 9 @ c:/totalflatline-2.0/node_modules/karma-jasmine/lib/boot.js:126 @ c:/totalflatline-2.0/node_modules/karma-jasmine/lib/adapter.js:171 @ http://localhost:9876/karma.js:210 @ http://localhost:9876/context.html:83 hantomjs 1.9.8 (windows 7): executed 2 of 2 (1 failed) (0.402 secs / 0.025 secs)
the 2 tests following. first 1 passes , other 1 gives above undefined error.
the 1 passes:
describe('testing mean main module', function() { var mainmodule; beforeeach(function() { mainmodule = angular.module('mean'); }); it('should registered', function() { expect(mainmodule).tobedefined(); console.log('success!'); }); });
the 1 fails:
describe('testing shared stats controller', function(){ var sharedcontroller,$scope; beforeeach(function(){ // load module you're testing. module('mean'); inject(function($rootscope,$controller){ // create scope object use. $scope = $rootscope.$new(); sharedcontroller = $controller('shared.statscontroller',{ $scope:$scope }); }); }); it('should contain user object',function(){ // user cannot undefined expect($scope.test).toequal('yoo'); }); });
the angular controller looking this:
// create 'shared' controller angular.module('shared').controller('shared.statscontroller', [ '$scope', function($scope) { $scope.test = 'yoo'; } ]);
angular version 1.4 , karma dependencies versions:
"karma": "~0.12.23", "karma-jasmine": "~0.2.2", "karma-phantomjs-launcher": "~0.1.4",
i have been breaking neck on day. hope more knowledge testing angular can me out.
your angular controller isn't instantiated properly. 'get' or load module, use angular.module('modulename', [])
, second argument being array dependencies. so, should be:
angular.module('shared', []).controller('statscontroller', [ '$scope', function($scope) { $scope.test = 'yoo'; } ]);
Comments
Post a Comment