2014-07-11 3 views
1

없는 것은 내가 뭐하는 거지입니다 : whenDELETE : 정의되지 않은이 여기에 기능

controller.js

var app = angular.module('app', [ 'angularFileUpload' ]); 

app.controller('MyCtrl', [ '$scope', '$http', '$timeout', '$upload', 
      function($scope, $http, $timeout, $upload) { 
    $scope.something; 
    $scope.deleteFile = function(fileId) { 

    $http({ 
     method : 'DELETE', 
     url : "http://xxx.xxx.xx.xx:8080/fileId 
    }).success(function(response) { 
     scope.something=response; 
     console.log(response); 
    }); 
}); 

controllerspec.js

ddescribe("MyCtrl test cases ",function(){ 
    var scope , controller ,httpBackend; 
    beforeEach(angular.mock.module('app')); 

    beforeEach(angular.mock.inject(function($rootScope,$controller,$http, $timeout, $upload){ 
    scope = $rootScope.$new(); 
    httpBackend = $http; 
    $controller('MyCtrl',{ 
     $scope:scope, 
     $http:httpBackend, 
     $timeout:$timeout, 
     $upload:$upload 
    }); 

    })); 

    it("should delete selected file ",function(){ 
    /*HERE*/ httpBackend.when('DELETE','http://xxx.xxx.xx.xx:8080/1').respond 
     ({ 
      "deleted" 
     }); 
     httpBackend.flush(); 

     scope.deleteFile(1); 

     expect(scope.something).toBeDefined(); 


}); 

나는 TypeError: Undefined is not a function at line "HERE"를 얻을.

무엇이 누락 되었습니까?

+0

'httpBackend.expectDELETE ('yoururl')'? – putvande

+0

나는 그것을 또한 시도했다. 그것은 나에게 같은 오류를 주었다. – vijay

답변

1

당신 때문에 다음 코드 의 조각 대신

beforeEach(angular.mock.inject(function($rootScope,$controller,$http, $timeout, $upload){ 
scope = $rootScope.$new(); 
httpBackend = $http; 
$controller('MyCtrl',{ 
    $scope:scope, 
    $http:httpBackend, 
    $timeout:$timeout, 
    $upload:$upload 
}); 

사용의 코드에서 오류가

beforeEach(angular.mock.inject(function($rootScope,$controller,$httpBackend, $timeout, $upload){ 
    scope = $rootScope.$new(); 
    httpBackend = $httpBackend; 
    $controller('MyCtrl',{ 
     $scope:scope, 
     $timeout:$timeout, 
     $upload:$upload 
    }); 

은 $ httpBackend HTTP 요청을 조롱에 사용되는, 필요 컨트롤러 내부를 주입 없습니다 대신 $ http 서비스를 삽입 할 수 있습니다. 또한 httpBackend 서비스를 주입하지 않았고 그 기능을 사용하려 했으므로 오류가 발생했습니다. 그 이유는 정의되지 않은 오류가 발생했기 때문입니다.