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 に答える