angularjs - Error: Unexpected request: POST Karma-jasmine-angular -


the actual scenario when trying test angular js controller , service in first call get ajax request , on success call user service contains post ajax request. when run test got following error.

error: unexpected request: post http://localhost:8080/services/access no more request expected in /home/test/webstormprojects/test2/bower_components/angular-mocks/angular-mocks.js

i found similar questions in stack overflow not fit scenario. these following questions found.

unexpected request: no more request expected @ $httpbackend

mocking $httpbackend - how handle "unexpected request, no more request expected"?

angularjs $httpbackend - "no more request expected" error

here code did , try.

test.js

describe('login controller',function(){    beforeeach(module('app'));    var httpbackend,ctrl, scope,userservice;    beforeeach(inject(function($controller, $rootscope,$httpbackend,$injector,_userservice_) {      scope = $rootscope.$new();     httpbackend = $injector.get('$httpbackend');      userservice = _userservice_;      ctrl = $controller('logincontroller', {       $scope: scope     });   }));    it('should set authentication true', function() {      scope.authenticateuser();      var username = 'test';     var password = '123456';     var requesttoken = "";      httpbackend.whenget('http://localhost:8080/services/request').respond(200,{       status : 0     });      httpbackend.expect('post', 'http://localhost:8080/services/access').respond(200, {       status:"success"     });      var returnedpromise = userservice.authenticateuser(username,password,requesttoken);      httpbackend.flush();      expect(scope.result).toequal(0);      httpbackend.verifynooutstandingexpectation();     httpbackend.verifynooutstandingrequest();    }); }); 

app.js

app.controller('logincontroller', function logincontroller($scope,$http,userservice) {   $scope.test="hello";   $scope.authenticateuser = function(){     var username = 'test';     var password = '123456';     var token = "";      $http.get('http://localhost:8080/services/request').success(function(data) {         $scope.result = data.status;         userservice.authenticateuser(username, password, "").then(function(response){});     });   }; }); 

userservice.js

userservice.authenticateuser = function(username, password,requesttoken){              var status = $http({                 method: 'post',                 url: "http://localhost:8080/services/access",                 data: $.param({username:username,password:password,request_token:requesttoken}),                 headers: {'content-type': 'application/x-www-form-urlencoded'}              }).success(function(data, status, headers, config) {                 return data;              }).               error(function(data, status, headers, config) {                 return null;             });             return status;         };          return userservice; 

i new karma-jasmine-angular test , struggling kind of scenarios. please provide me proper guidance resolve kind of testing situation. appreciate. if possible please suggest me demos test service called on success of service. in advance.

you forgot fake backend respond when post request http://localhost:8080/services/access. on test test expected post request whether made or correct params. add in fake response , work perfectly

httpbackend     .when('post', 'http://localhost:8080/services/access')     .respond(200, {         status: "success" }); 

link fiddle preference.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -