Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在Angular controller中使用$httpBackend进行测试,不返回模拟响应_Angularjs_Unit Testing_Karma Jasmine - Fatal编程技术网

Angularjs 在Angular controller中使用$httpBackend进行测试,不返回模拟响应

Angularjs 在Angular controller中使用$httpBackend进行测试,不返回模拟响应,angularjs,unit-testing,karma-jasmine,Angularjs,Unit Testing,Karma Jasmine,我对Angular JS的测试非常陌生,在对控制器进行单元测试时遇到了一些问题(我使用的是Karma和Jasmine) 我知道最好的做法是分别测试资源工厂和控制器,但现在,我正试图同时测试它们,因为我的控制器具有调用这些资源的函数 angular.module('myApp.controllers') .controller('myDocumentsController', ['documentResourceFactory', '$scope', function(documentRe

我对Angular JS的测试非常陌生,在对控制器进行单元测试时遇到了一些问题(我使用的是Karma和Jasmine)

我知道最好的做法是分别测试资源工厂和控制器,但现在,我正试图同时测试它们,因为我的控制器具有调用这些资源的函数

  angular.module('myApp.controllers')
  .controller('myDocumentsController', ['documentResourceFactory', '$scope', function(documentResourceFactory, $scope) {  

    $scope.queryDocuments = function(){
      $scope.userDocuments = documentResourceFactory.query($scope.queryParams(), function(){
        $scope.fetchingDocuments=false
      })
    };

    $scope.queryParams = function() {
      if($scope.feedItem) {
        return { "feed_item_id": $scope.feedItem.id }  
      } else {
        return { "entry_id": $scope.entry.id }
      }
    };
}])
这是我为这个控制器做的测试,我似乎无法工作:

describe('myDocumentsController', function(){
    var $scope, $httpBackend, $rootScope, $controller, documentResourceFactory;

beforeEach(angular.mock.module('myApp'));

beforeEach(angular.mock.inject(function(_$rootScope_, _$controller_, _$httpBackend_, _documentResourceFactory_){
    $rootScope = _$rootScope_;
    $scope = $rootScope.$new();
    $controller = _$controller_
    $httpBackend = _$httpBackend_;
    documentResourceFactory = _documentResourceFactory_
    $controller('myDocumentsController', {$scope: $scope});
}));


    describe('called when looking at feed item documents', function(){
    beforeEach(function(){
        $scope.feedItem = {
            "id": 3533669,
            "description": "Exhibit",
            "published_at": "2015-02-04T15:14:51.000Z",
            "published_at_to_i": 1423062891,
        };
    });

    //PASSING
    it('should be a feedItem on the scope', function(){
        expect($scope.feedItem).not.toBeNull();
    });

    //PASSING
    it('should have queryParams() returning "feed_item_id" when there is a feed item on the scope', function(){
        expect($scope.queryParams()).toEqual({"feed_item_id": 3533669});
    });


    // THIS TEST IS NOT PASSING
    it('should return a list of documents associated a feed item', function(){
        $httpBackend.when('GET', 'https://example.com/api/v1/my_documents?feed_item_id=3533669').respond([{}, {}]);

        $scope.$apply(function() {
            $scope.queryDocuments()
        });

        expect($scope.userDocuments.length).toEqual(2);
        $httpBackend.flush();
    }); 
});
这里的问题是,无论我做什么,$httpBackend永远不会被击中;因此,从不使用两个空对象进行响应。当我测试这个时,我得到一个空数组,它告诉我它“期望0等于2”。看来测试运行得很好,我就是无法从$httpBackend得到响应。当我在开发服务器上运行它时,所有这些代码都运行良好


我做错了什么?提前谢谢

你在检查长度后冲水,而不是之前冲水。响应仅在刷新后可用。感谢您的快速响应!出于好奇,同花顺是干什么的?根据文档,看起来所有$httpBackend请求都已排队,然后flush一次执行它们以模拟异步调用?是这样吗?是的。基本上,flush告诉httpBackend将收到的请求的响应发送回来。太好了!谢谢-我真的很感激!