whenDELETE:undefined不是函数

时间:2014-07-11 07:55:34

标签: angularjs karma-runner httpbackend

这是我正在做的事情:

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"

我错过了什么?

1 个答案:

答案 0 :(得分: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服务并试图使用它的函数来解决为什么你得到了未定义的错误。