lundi 28 septembre 2015

how to unit test for http request inside a function in directive

i have made a function inside a http request is made for get and then post request how can i test that method here is my directive controller funtion:

           var getConfig = function () {
               if (!$scope.json) {
                 $http.get('/schools/' + $scope.school.id + '/config?                deployment_uuid=' + $scope.schoolId)
                   .then(function (response) {
                    $scope.school.config = JSON.stringify(response.data, null, 4);
                    });
                   }
      };

       $scope.disable = function () {
        getConfig();
        $http.post('/school/' + $scope.school.id + '/update', {
          deployment_id: $scope.schoolId,
          component: $scope.schoolName,
          is_enabled: false,
          config: JSON.parse($scope.school.config)
        });
      };

testcase:

       describe('controller', function () {
         fit('should POST payload  for a scenario to disable', function () {
         $scope.school.config = JSON.stringify(mockSchoolConfig, null, 4);
         $scope.disable();
         $scope.$digest();
        $httpBackend.expect('POST', '/school/' + $scope.school.id + '/update', {
        deployment_id: $scope.schoolId,
        component: $scope.schoolName,
        is_enabled: false,
        config: $scope.school.config
      }).respond(200);
      //$httpBackend.flush();
  });
});

Error: Unexpected request: GET /s/create-alarm/config?school_uuid=170bf60e-0153-4615-9a4e-a6bc3ad546ea
No more request expected

how can test this funtion and http request?

Aucun commentaire:

Enregistrer un commentaire