javascript - Karma + PhantomJS TypeError: undefined is not an object (evaulating scope.jackpot) -
i still new unit testing, , honest, there isn't think of testing, cannot build app unless have @ least 1 test case, attempted make simple test case could, on smallest block of code in controller, , doesn't seem working.
i believe it's error in test case, , not in controller's code itself, because when view app in browser grunt serve
console shows no errors.
this error gives me:
phantomjs 2.1.1 (linux 0.0.0) controller: mainctrl should attach list of jackpot scope failed /home/elli0t/documents/yeoman projects/monopoly/app/bower_components/angular/angular.js:3746:53 foreach@[native code] foreach@/home/elli0t/documents/yeoman projects/monopoly/app/bower_components/angular/angular.js:323:18 loadmodules@/home/elli0t/documents/yeoman projects/monopoly/app/bower_components/angular/angular.js:3711:12 createinjector@/home/elli0t/documents/yeoman projects/monopoly/app/bower_components/angular/angular.js:3651:22 workfn@/home/elli0t/documents/yeoman projects/monopoly/app/bower_components/angular-mocks/angular-mocks.js:2138:60 typeerror: undefined not object (evaluating 'scope.jackpot') in /home/elli0t/documents/yeoman projects/monopoly/test/spec/controllers/main.js (line 20) /home/elli0t/documents/yeoman projects/monopoly/test/spec/controllers/main.js:20:17 phantomjs 2.1.1 (linux 0.0.0): executed 1 of 1 (1 failed) error (0.04 secs / 0.007 secs)
this test case:
it('should attach list of jackpot scope', function () { expect(scope.jackpot.length).tobe(2); });
and block of code i'm attempting run test on:
var countinjackpot = localstorageservice.get('jackpot'); $scope.jackpot = countinjackpot || [ { letter: '$', prize: '$1,000,000 cash', numbers: ['$611a','$612b','$613c','$614d','$615e','$616f','$617g','$618f'], count: [0,0,0,0,0,0,0,0] }, { letter: '?', prize: '$500,000 vacation home', numbers: ['?619a','?620b','?621c','?622d','?632e','?624f','?625g','?626h'], count: [0,0,0,0,0,0,0,0] } ];
for time being, want write 1 simple test case, let me build app. i'm studying unit testing, still don't feel ready write more complex test cases on own. save later.
i have included entire contents of files in gist reference, if needed, , can include contents of karma.conf.js if necessary.
i know error has come in many other questions on site, have been looking through them day today , none of them helped me problem made own post. if other information needed solve problem, please let me know. thanks!
within test case, scope should $scope?
or
you haven't setup testing environment load in controller.
here example of mine on testing controller... angular makes setup little iffy learn, once understand flow. it's pretty great :)
i'm going try , add many comments explain each piece can... let me know if need clarification. might using jasmine, keep in mind, mocha, im using angular mock library loaded in via karma.conf.
describe('mycontroller', function() { var $scope, createcontroller; // runs before each test. re-extantiating controller want test. beforeeach(inject(function($injector) { // hold of scope (i.e. root scope) $scope = $injector.get('$rootscope'); // $controller service used create instances of controllers var $controller = $injector.get('$controller'); createcontroller = function() { // creates controller instance of our controller. // injecting $scope have access // after controllers code runs return $controller('myctrl', { '$scope': $scope }); }; })); describe('#myfunction', function() { it('jackpot should contain 2 objects', function() { expect($scope.jackpot.length).to.equal(2); }); }); });
hope helped. here's of resources used learn :) luck!
Comments
Post a Comment