xmlhttprequest - Mocking AngularJS XHR requests for testing using Jasmine -


ok, pretty sure weird behaviour - , have no idea going on...

code below.

i running jasmine spec on possibly simple angularjs controller ever, makes call function (getsuppliers) upon initialisation, in turn makes http request, have mocked out using $httpbackend. weirdness happens when run test. fails expected undefined equal 'list of suppliers'.

so stuck in debugger;s (1 , 2) , ran spec in chrome. weird part. debugger 2 gets hit before debugger 1, meaning getsuppliers function returns value of undefined before mock has chance it's magic. record, when continue on , let debugger 1 come well, data has correct value of "list of suppliers", if mock run in right place should sweet. have idea hell going on here?

spec.js

describe("supplier controller", function(){     ctrl = null;     scope = null;     httpbackend = null;     suppliercontroller = null;      beforeeach(inject(function($controller,$rootscope,$httpbackend) {         scope = $rootscope.$new();         ctrl = $controller;         httpbackend = $httpbackend;          httpbackend.expectget('/suppliers.json').respond("list of suppliers");         suppliercontroller = ctrl('supplierctrl', { $scope: scope});         $httpbackend.flush();     }));      it("gets list of suppliers", function(){         expect(scope.suppliers).toequal( "list of suppliers" );     }); }); 

supplier.js

function supplierctrl($scope, $http) {     $scope.suppliers = getsuppliers($http); }  function getsuppliers($http){     var suppliers;     $http.get('/suppliers.json').success(function(data) {         debugger; // debugger 1         suppliers = data;     }).error(function(){ suppliers = "something else"; } );     debugger; // debugger 2     return suppliers; }; 

thanks wisdom...


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -