Struggling to get started on AngularJS unit testing -
i followed post (http://gonehybrid.com/how-to-write-automated-tests-for-your-ionic-app-part-2/) create simple unit test using karma & jasmine ionic controller, keep getting undefined errors while stated objects have been defined. i'm missing obvious? way, i'm able run referenced tests blog above makes me think i'm missing in mine.
errora follows:
- typeerror: undefined not object (evaluating 'authmock.login') in /users/projects/app/tests/unit-tests/login.controller.tests.js (line 65)
- typeerror: undefined not object (evaluating 'deferredlogin.resolve') in /users/projects/app/tests/unit-tests/login.controller.tests.js (line 71)
- typeerror: undefined not object (evaluating 'deferredlogin.reject') in /users/projects/app/tests/unit-tests/login.controller.tests.js (line 79)
here's controller:
angular.module('app').controller('loginctrl', function($scope, $state, $ionicpopup, $auth) { $scope.logindata = {}; $scope.user = { email: '', password: '' }; $scope.dologin = function(data) { $auth.login(data).then(function(authenticated) { $state.go('app.tabs.customer', {}, {reload: true}); }, function(err) { var alertpopup = $ionicpopup.alert({ title: 'login failed!', template: 'please check credentials!' }); }); }; }); here's test:
describe('loginctrl', function() { var controller, deferredlogin, $scope, authmock, statemock, ionicpopupmock; // load module our app beforeeach(angular.mock.module('app')); // disable template caching beforeeach(angular.mock.module(function($provide, $urlrouterprovider) { $provide.value('$ionictemplatecache', function(){} ); $urlrouterprovider.deferintercept(); })); // instantiate controller , mocks every test beforeeach(angular.mock.inject(function($controller, $q, $rootscope) { deferredlogin = $q.defer(); $scope = $rootscope.$new(); // mock dinnerservice authmock = { login: jasmine.createspy('login spy') .and.returnvalue(deferredlogin.promise) }; // mock $state statemock = jasmine.createspyobj('$state spy', ['go']); // mock $ionicpopup ionicpopupmock = jasmine.createspyobj('$ionicpopup spy', ['alert']); // instantiate logincontroller controller = $controller('loginctrl', { '$scope': $scope, '$state': statemock, '$ionicpopup': ionicpopupmock, '$auth': authmock }); })); describe('#dologin', function() { // call dologin on controller every test beforeeach(inject(function(_$rootscope_) { $rootscope = _$rootscope_; var user = { email: 'test@yahoo.com', password: 'test' }; $scope.dologin(user); })); it('should call login on $auth service', function() { expect(authmock.login).tohavebeencalledwith(user); }); describe('when login executed,', function() { it('if successful, should change state app.tabs.customer', function() { deferredlogin.resolve(); $rootscope.$digest(); expect(statemock.go).tohavebeencalledwith('app.tabs.customer'); }); it('if unsuccessful, should show popup', function() { deferredlogin.reject(); $rootscope.$digest(); expect(ionicpopupmock.alert).tohavebeencalled(); }); }); }) }); here's karma config:
files: [ '../www/lib/ionic/js/ionic.bundle.js', '../www/lib/angular-mocks/angular-mocks.js', '../www/js/*.js', '../www/js/**/*.js', 'unit-tests/**/*.js' ],
i think controller tests undefined. try replace first function , check if defined.
it('controller defained', function() { expect($controller).tobedefined(); }); if isn't, try call controller with:
$controller = _$controller_;
Comments
Post a Comment