angularjs - code to used to test in jasmine -
i need pass service call test using jasmine.js
the code below angular.js service call:
var myapp = angular.module('myapp',[]); app.controller('myctrl', function($scope, $http) { $http.get("http://www.w3schools.com/angular/customers.php") .success(function(response) {$scope.names = response.records;}); });
how pass unit test using jasmine.js? tried wasn't successful:
describe('myctrl', function() { var $httpbackend, scope, createcontroller, authrequesthandler; // set module beforeeach(module('myapp')); alert("hello there!"); beforeeach(inject(function ($injector) { // set mock http service responses alert("hello there!"); $httpbackend = $injector.get('$httpbackend'); // backend definition common tests alert("hello there!"); authrequesthandler = $httpbackend.whenget('get', 'http://www.w3schools.com/angular/customers.php') .respond(true); // hold of scope (i.e. root scope) $rootscope = $injector.get('$rootscope'); // $controller service used create instances of controllers var $controller = $injector.get('$controller'); createcontroller = function() { return $controller('myctrl', {'$scope' : scope}); }; }) ); /* aftereach(function () { $httpbackend.verifynooutstandingexpectation(); $httpbackend.verifynooutstandingrequest(); }); */ it('should fetch authentication token', function() { //create expectation $httpbackend.expectget('get','http://www.w3schools.com/angular/customers.php') var controller = createcontroller() $httpbackend.flush(); expect(scope.names).tobetruthy(true); }); });
try this:
describe('myctrl', function() { var $httpbackend, $scope, authrequesthandler, createcontroller, url; $httpbackend = void 0; $scope = void 0; createcontroller = void 0; authrequesthandler = void 0; url = void 0; beforeeach(module('myapp')); alert('hello there!'); beforeeach(inject(function($injector, $controller, $rootscope) { var controllerservice; alert('hello there!'); $httpbackend = $injector.get('$httpbackend'); alert('hello there!'); url = 'http://www.w3schools.com/angular/customers.php'; $scope = $rootscope.$new(); controllerservice = $controller; createcontroller = function() { return controllerservice('myctrl', { '$scope': $scope }); }; })); /* aftereach(function () { $httpbackend.verifynooutstandingexpectation(); $httpbackend.verifynooutstandingrequest(); }); */ it('should fetch authentication token', function() { $httpbackend.whenget(url).respond(); $httpbackend.expectget(url).respond(); $httpbackend.flush(); createcontroller(); expect($scope.names).tobetruthy(true); }); });
Comments
Post a Comment