2014-07-11 55 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服務,並試圖使用它的函數,爲什麼你得到未定義的錯誤。

相關問題