JavaScript - AngularJS promise not returning anything -
i trying return simple http post on unit test jasmine
seems not work. application works fine on web. have tested several isolated functions. won't work.
describe('service: auth',function(){ beforeeach(function () { module('ui.router'); module('main'); module('users'); }); var authfactory, httpbackend; beforeeach(inject(function($httpbackend, _authfactory_) { httpbackend = $httpbackend; authfactory = _authfactory_; })); it('should return post', function() { authfactory.signin({inputuser: {username: "admin"}, passinput: {password: "adminpass"}}).then( function(result) { console.log('======== success ========'); console.log(result); }, function(err) { console.log('======== error ========'); console.log(err); }, function(progress) { console.log('======== progress ========'); console.log(progress); } ); console.log('http call finished.'); expect(1).tobe(1); }); });
and here factory:
angular.module('users').factory('authfactory', ['$http', function($http) { var authfactory = {}; authfactory.signin = function(data) { return $http.post('http://127.0.0.1:3000/api/authfactoryserv/signin', data); }; authfactory.signout = function(data) { return $http.post('http://127.0.0.1:3000/api/authfactoryserv/signout', data); }; return authfactory; }]);
this get:
phantomjs 1.9.8 (windows 7 0.0.0): executed 0 of 1 success (0 s log: object{$$state: object{status: 0}, success: function (fn) { ... }, error: function (fn) { ... }} phantomjs 1.9.8 (windows 7 0.0.0): executed 0 of 1 success (0 s log: 'http call finished.' phantomjs 1.9.8 (windows 7 0.0.0): executed 0 of 1 success (0 s phantomjs 1.9.8 (windows 7 0.0.0): executed 1 of 1 success (0 s phantomjs 1.9.8 (windows 7 0.0.0): executed 1 of 1 success (0 secs / 0.022 secs)
i have tested http calls via postman , returns data back. so... doing wrong?
thank you.
the authfactory.signin
asynchronous , returns promise - test function finished before promise resolved.
you need tell jasmine should wait asynchronous test complete:
it('should return post', function(done) { var result = authfactory.signin({inputuser: {username: "admin"}, passinput: {password: "adminpass"}}).then( function(result) { console.log('======== success ========'); console.log(result); }, function(err) { console.log('======== error ========'); console.log(err); }, function(progress) { console.log('======== progress ========'); console.log(progress); } ); result.then(function(){ console.log('http call finished.'); expect(1).tobe(1); }).finally(done); //here hook jasmine callback promise chain });
Comments
Post a Comment