Wednesday, 15 June 2011

javascript - Angular Unit Testing - httpBackend JSONP -



javascript - Angular Unit Testing - httpBackend JSONP -

i have spent way much time on this. have simple angular app gets jsonp remote server , displays on screen.

i having hardest time creating unit test jasmine.

here app

var pdapp = angular.module('pdapp', []); pdapp.controller('pdcontroller', function ($scope, $http) { var jsonpurl = "http://10.1.20.377/products/1/167?cb=json_callback"; $http.jsonp(jsonpurl).success(function(data) { $scope.pagecontent = data.pagecontent; $scope.cartcontent = data.cartcontent; $scope.content = data.productcontent; }); });

and here unit test

describe('mytest', function () { var mainctrl, scope, httpbackend; beforeeach(module('pdapp')); module('pdapp', function($provide) { $provide.value('defaultcontent', defaultjson); }); beforeeach(inject(function($controller, $rootscope, $httpbackend) { httpbackend = $httpbackend; scope = $rootscope.$new(); mainctrl = $controller('pdcontroller', { $scope: scope }); })); it("product name should set", function(){ httpbackend.whenjsonp("/products/1/167").respond( { "name" : "prod name", "desc" : "long description here" } ); expect(scope.pagecontent.name).toequal("prod name"); }); });

i notice forgot httpbackend.flush();, response not flush out.

javascript angularjs unit-testing jasmine

No comments:

Post a Comment