0

I have a simple controller method like this

$scope.testMethod= function (urlPost) {
    var obj= {
            headers: {
                'Content-Type': 'application/json'
            },
            method: 'POST',
            data: $scope.prjObj,
            url: urlPost
        };
      if (obj!= undefined) {
           $http(obj).then(function (response) {
           console.log(respone)
          }) 
      //rest of the code
      }}

I am trying to test this method

 beforeEach(angular.mock.inject(function ($injector) {
            $httpBackend = $injector.get('$httpBackend');
            var x ={
                key:'value'
            }
            $httpBackend.when("POST",'/path/to/url',x).respond(200, ["Toyota"]);
 // Rest of the code
})

it("Should check for the testMethod",function(){

        var x ={key:"value"
        }
        $httpBackend.expect('POST','/path/to/url',x).respond({ hello: 'World' });
        createController();
        scope.testMethod("pathToUrl");

        $httpBackend .flush()

    })

I follower this ,this,this , this & numerous online resources, but it is still throwing

Error: Unexpected request: [object Object] undefined
    Expected POST /path/to/url
        at $httpBackend (node_modules/angular-mocks/angular-mocks.js:1323:9)
        at ChildScope.$scope.addEditProject (testCtrl.js:9:11770)
        at UserContext.<anonymous> (testCtrl-spec.js:201:15)

Any idea where I am making mistake?

brk
  • 48,835
  • 10
  • 56
  • 78

1 Answers1

0

The issue was with the $http & $httpBackend. The controller was expecting $http but it was injecting $httpBackend

Have created a new variable and & injected like this

$http=$injector.get("$http");
brk
  • 48,835
  • 10
  • 56
  • 78