1

这是我正在做的事情:

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"

我错过了什么?

4

1 回答 1

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 服务并试图使用它的功能,这就是你得到未定义错误的原因。

于 2014-07-11T09:22:22.687 回答