javascript - undefined $scope variable inside function in jasmine test -
i started learn unit test angular apps. , faced problem. can not take scope
variable inside executed function. here factory code
angular.module('app').factory('authenticationservice', authenticationservice); authenticationservice.$inject = ['$http']; function authenticationservice($http) { var service = {}; service.login = login; return service; function login(data, callback) { $http({ method: 'post', url: config.geturl('auth/login'), data: data }).then(function (response) { callback(response); }, function (error) { callback(error); }); }
part of controller file. yet wan test login
function
function authctrl($scope, $location, authenticationservice) { var vm = this; vm.login = login; vm.datalogin = { user_id: '', password: '', }; function login() { vm.dataloading = true; authenticationservice.login(vm.datalogin, function (response) { if (response.status == 200) { if (response.data.error_code == 'auth.credentials.invalid') { vm.invalidcredentials = true; } else { vm.invalidcredentials = false; if (response.data.session_state == 'otp_required') { vm.usernumber = response.data.user_phone; $localstorage['session_token'] = response.data.session_token; vm.needform = 'someform'; } else { authenticationservice.setcredentials(response.data); $state.go('dashboard'); } vm.dataloading = false; } } }); } } });
and spec.js
describe('authctrl, ', function() { var $scope, ctrl; var authsrvmock; var mockjson = { user_id: '001', session_token: 'some_token' }; var mocklogindata = { user_id: '0000102', password: '123456' }; var mockresponsedata = { data: { "session_expires": 1453822506, "session_state": "otp_required", "session_token": "tokennnn", "status": "success", "user_id": "0000102", "user_phone": "+7 (xxx) xxx-xx-89" }, status: 200 }; beforeeach(function () { authsrvmock = jasmine.createspyobj('authenticationservice', ['login', 'logout']); module('app'); inject(function ($rootscope, $controller, $q) { $scope = $rootscope.$new(); authsrvmock.login.and.returnvalue(mockresponsedata); ctrl = $controller('authctrl', { $scope: $scope, authenticationservice: authsrvmock }); }); }); it('should call login function , pass dashboard', function () { ctrl.login(); expect(authsrvmock.login).tohavebeencalled(); // until works here fine }); });
but after want test vm.invalidcredentials
, if write
expect(ctrl.invalidcredentials).tobe(false)
i error expected undefined false.
why can't see variables?
bit of noob myself @ jasmine, i'm guessing it's because need promise login()
return in jasmine.
look using $q.defer()
, or $httpbackend
.
Comments
Post a Comment