javascript - How to unit test an angularjs promise -
i unit test promise this:
function getsongs() { var d = $q.defer(); var url = 'http://localhost:3002/songs'; $http({ method: 'get', url: url }) .success(function(data) { d.resolve(data); }) .error(function(data) { d.reject(data); }); return d.promise; }
this returns , arrays of objects. used in controller calling getsongs function:
var vm = this; vm.songlist = []; vm.init = function () { playerscreenservice.getsongs() .then(getsongssuccess); }; vm.init();
thank much.
take @ $httpbackend. test this:
describe('playerscreenservice', function () { it('should send http request', inject(function (playerscreenservice, $httpbackend) { $httpbackend.whenget('http://localhost:3002/songs').respond(200, {...}); var getsongssuccess = jasmine.createspy('getsongssuccess'); var getsongserror = jasmine.createspy('getsongserror'); playerscreenservice.getsongs() .then(getsongssuccess, getsongserror); $httpbackend.flush(); expect(getsongssuccess).tohavebeencalledwith({...}); expect(getsongserror).not.tohavebeencalled(); })); });
Comments
Post a Comment