AngularJs $httpbackend.whenPOST regular expression not working

Issue

am getting an error
Error: Unexpected request: POST data/employee/1
No more request expected

Am using angular-mocks, angular-ui-route in my app.config i’ve got my $stateProvider routes and my mocks the main one with the issue being the one below:

$httpBackend.whenPOST(/data\/employee\/(\d+)/, {}, {},['id']).respond(function(method, url, data, headers, params){

        console.log('Received these data:', method, url, data, headers, params);

        return [200, {}, {}]
});

Calling via controller:

app.controller("employeeController", ['$scope','$http', "$state", "$stateParams", function($scope, $http, $state, $stateParams){

   $http.post("data/employee/1").then(function(response){


   })
 })

What might be the issue?

Solution

In order for the mock $httpBackend.whenPOST(url, [data], [headers], [keys]) to work optional parameters data and header need to be declared as undefined where they are not used and not just as empty objects {}.

Answered By – Sami

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.