Search code examples
javascriptangularjskarma-runnerkarma-mocha

Error: Unexpected request: POST karma


I am getting the below error when i run my test cases, Error: Unexpected request: POST data/json/api.json

it("should $watch value", function(){
      var request = '/data/json/api.json';
      $httpBackend.expectPOST(request).respond(200);

      var iplScore = 10;
      var controller = new controller(iplScore);
      $scope.cricketScore = 0;
      $scope.$digest();
      $scope.cricketScore = 1;
      assert.equal(iplScore, 0);
 });

Below is my controller code:

$scope.$watch("cricketScore", function(newValue, oldValue) {
     $scope.iplScore = oldValue;
});

Can anyone help me out why am i am getting the above error, even though i have mocked the api via $httpBackend


Solution

  • You have not mocked your API calls. Put the following into the beforeEach():

    beforeEach(function () {
        $httpBackend.when('POST', '/data/json/api.json').respond(200, {
            status: "success"
        });
    });