angularjs - Migrating from $http to Restangular, tests failing -
i have following controller:
'use strict'; angular.module('app') .controller('loginctrl', function ($scope, $state, login, localstorageservice, message, authservice) { $scope.submit = function() { if (this.username) { var credentials = { username: this.username, password: this.password }; login.post(credentials).then(function(data) { if (data.status === 200) { authservice.loadcustomer(data.data); $state.go(authservice.nextpath() || 'curator'); } }, function(data) { if (data.status === 401) { $scope.password = ''; message.error('wrong e-mail/password combination'); } else { message.error('an error occured while logging in.'); } }); } } });
which migrated using $http restangular service:
!(function(window, angular){ 'use strict'; /** * @ngdoc service * @name app.login * @description * # login * service in app, rest interface login */ function login(restangular) { return restangular.service('login'); } angular.module('app') .service('login', login); }(window, window.angular));
the tests migrated well:
'use strict'; describe('controller: loginctrl', function () { // load controller's module beforeeach(module('app')); var mainctrl, scope, $state, $api, login, mockedresponses, $httpbackend, localstorageservice; // initialize controller , mock scope //use double underline suffix , prefix prevent shadowing outer scope dependencies //with inner scope ones beforeeach(inject(function ($controller, $rootscope, _$api_, _message_, _login_, _$state_, _$httpbackend_, _mockedresponses_, _localstorageservice_) { $httpbackend = _$httpbackend_; mockedresponses = _mockedresponses_; $state = _$state_; $api = _$api_; login = _login_; localstorageservice = _localstorageservice_; $httpbackend.whenget(/views\/login.html/).respond(200, ''); $httpbackend.whenget(/views\/curator.html/).respond(200, ''); $httpbackend.whenget(/views\/navigation.html/).respond(200, ''); scope = $rootscope.$new(); mainctrl = $controller('loginctrl', { $scope: scope, login: login, message: _message_, $state: $state, $httpbackend: $httpbackend, $stateparams: {} }); })); aftereach (function () { $httpbackend.verifynooutstandingexpectation(); $httpbackend.verifynooutstandingrequest(); }); it('should pass login credentials on submit', function(){ $httpbackend.whenpost(/login/).respond(mockedresponses.getsuccessfulloginresponse); var spy = spyon(login, 'post').and.callthrough(); scope.username = 'pony@rider.com'; scope.password = 'yiiihaaa'; scope.submit(); $httpbackend.flush(); scope.$digest(); expect(spy.calls.argsfor(0).username).toequal(scope.username); expect(spy.calls.argsfor(0).password).toequal(scope.password); }); it('should remove password if authentication failed', function(){ $httpbackend.whenpost(/login/).respond(mockedresponses.getfailedloginresponse); $httpbackend.flush(); scope.username = 'failedpony@rider.com'; scope.password = 'yiiihaaa'; scope.submit(); $httpbackend.flush(); scope.$digest(); expect(scope.password).toequal(''); }); it('should alert user if authentication failed', function(){ $httpbackend.whenpost(/login/).respond(mockedresponses.getfailedloginresponse); scope.username = 'failedpony@rider.com'; scope.password = 'yiiihaaa'; scope.submit(); $httpbackend.flush(); $rootscope.$apply(); expect(scope.error).tobedefined(); }); });
but reason wasn't able understand yet, tests failing because of exception within restangular code:
typeerror: cannot read property 'replace' of undefined @ /home/oleg/dev/dashboard/app/components/restangular/dist/restangular.js:680:32 @ function.reduce (/home/oleg/dev/dashboard/app/components/lodash/dist/lodash.js:3734:25) @ path.base (/home/oleg/dev/dashboard/app/components/restangular/dist/restangular.js:649:27) @ path.fetchurl (/home/oleg/dev/dashboard/app/components/restangular/dist/restangular.js:688:36) @ array.elemfunction (/home/oleg/dev/dashboard/app/components/restangular/dist/restangular.js:1120:45) @ bound (/home/oleg/dev/dashboard/app/components/lodash/dist/lodash.js:729:21) @ array.postfunction (/home/oleg/dev/dashboard/app/components/restangular/dist/restangular.js:1201:52) @ object.bound (/home/oleg/dev/dashboard/app/components/lodash/dist/lodash.js:729:21) error: unflushed requests: 2 @ function.$httpbackend.verifynooutstandingrequest (/home/oleg/dev/dashboard/app/components/angular-mocks/angular-mocks.js:1494:13) @ object.<anonymous> (/home/oleg/dev/dashboard/test/spec/controllers/login.js:46:20)
looks there wrong restangular's baseurl, try setting 1 explicitly. may help:
angular.module('app').config(function(restangularprovider) { restangularprovider.setbaseurl('http://www.yourdomain.com'); });
regards
Comments
Post a Comment