Flushing successful mock POST request using Jasmine does not execute the AngularJS success function -
this angularjs post.js:
angular.module("postpageapp", ["baseapp"]) .controller("mainctrl", ["$http", "$window", "baseservice", function($http, $window, baseservice) { var self = this; self.add = function() { baseservice.add.post(self.post, function() { self.cerrormessages = baseservice.cerrormessages; }); }; }]); this base.js:
angular.module("baseapp", []) .config(['$httpprovider', function($httpprovider) { $httpprovider.defaults.xsrfcookiename = 'csrftoken'; $httpprovider.defaults.xsrfheadername = 'x-csrftoken'; }]) .config(['$locationprovider', function($locationprovider){ $locationprovider.html5mode(true); }]) .factory("baseservice", ["$http", "$window", function($http, $window) { var self = this; self.posts = []; self.cerrormessages = []; /* function sets self.cerrormessages. after calling function, * should callback function on front-end * sets cerrormessage. */ self.accesserrors = function(data) { self.cerrormessages = []; (prop in data) { if (data.hasownproperty(prop)){ /* if (data[prop] != null && data[prop].constructor == object) { self.accesserrors(data[prop]); } else { */ self.cerrormessages.push(data[prop]); // } } } }; self.add = { post: function(post, callback) { $http.post("/posts/", post) .then(function(response) { $window.location.href = "/"; }, function(response) { self.accesserrors(response.data); callback(); }); } }; return self; }]); and test_post.js:
describe('controller: mainctrl', function() { beforeeach(module('postpageapp')); var ctrl, $loc; beforeeach(inject(function($controller, $location, $httpbackend, baseservice) { ctrl = $controller('mainctrl'); $loc = $location; mockbackend = $httpbackend; spyon(baseservice, 'add').and.callthrough(); baseservice = baseservice; })); it('should have add function', function() { expect(ctrl.add).tobedefined(); }); it('should able create post object', function() { $loc.path('/post'); ctrl.post = {'post':'test post'} mockbackend.expectpost('/posts/', ctrl.post) .respond(201, {'post':'testpost', 'posting': 1}); ctrl.add(); mockbackend.flush(); expect(baseservice.add).tohavebeencalled(); expect($loc.path()).toequal('/'); expect(ctrl.cerrormessages).tobeundefined(); }); }); now, when run karma start, returns this:
chromium 47.0.2526 (ubuntu 0.0.0) controller: mainctrl should able create valid post object failed expected spy add have been called. @ object.<anonymous> (/home/u/documents/cms/cmsapp/static/js/karma/tests/test_post.js:32:33) expected '/post' equal '/'. @ object.<anonymous> (/home/u/documents/cms/cmsapp/static/js/karma/tests/test_post.js:33:29) chromium 47.0.2526 (ubuntu 0.0.0): executed 3 of 3 (1 failed) (0 secs / 0.119 secchromium 47.0.2526 (ubuntu 0.0.0): executed 3 of 3 (1 failed) (0.163 secs / 0.119 secs) as can see, spy add expected called, since expected spy add have been called. printed on terminal, understanding, means not called, right?
also, printed expected '/post' equal '/'. onto terminal means url still @ '/post', correct?
any idea why url did not change , spy add not called?
if you're testing controller, only test controller. that's why it's called unit test. should mocking external services.
describe('controller: mainctrl', function() { var ctrl, mockbaseservice; beforeeach(function() { mockbaseservice = { cerrormessages: 'whatever', add: jasmine.createspyobj('baseservice.add', ['post']) }; mockbaseservice.add.post.and.callfake(function(something, cb) { cb(); // execute callback }); module('postpageapp'); inject(function($controller) { ctrl = $controller('mainctrl', { baseservice: mockbaseservice }); }); }); it('add calls through baseservice.add.post', function() { ctrl.post = 'something'; // adding because can't see anywhere else ctrl.add(); expect(mockbaseservice.add.post).tohavebeencalledwith(ctrl.post, jasmine.any(function)); expect(ctrl.cerrormessages).tobe(mockbaseservice.cerrormessages); }); });
Comments
Post a Comment