2015-05-05 83 views
0

我想在我的情况,以测试rootscope http请求如何在我的情况下测试一个http请求

我有类似

mainCont文件

$rootScope.testLoad = $http.get('/api/testapi/product'); 

TestCont文件

$rootScope.testLoad.success(function(product){ 
    console.log(product) 
}) 

测试文件

describe('test', function() { 
    var $httpBackend, $rootScope, scope, mainCont, testCont; 

    beforeEach(module('myApp')); 

    beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_) { 
     scope = _$rootScope_.$new(); 
     $rootScope = _$rootScope_; 
     $httpBackend = _$httpBackend_; 

     testCont = _$controller_('testCont', { 
      $scope: scope 
     }); 

     mainContr = _$controller_('mainCont', { 
      $scope: scope 
     }); 
    })); 

    describe('test http request', function() { 
     it('should check if the api is called', function() {  
      $httpBackend.expectGET('/api/testapi/product').respond(200); 
      //I am not sure how to test the call in testCont file. 
     }) 
    }) 
}); 

我不知道如何测试在testCont来电,因为当我运行测试, 我得到了一个错误说

TypeError: 'undefined' is not an object (evaluating '$rootScope.testLoad.success') 

谁能帮我解决这个问题?非常感谢!

回答

1

您已将testLoad定义为函数,因此您需要通过添加括号来调用它。将您的代码更改为

$rootScope.testLoad().success(function(product){ 
    console.log(product) 
}) 
+0

感谢您的帮助。我仍然得到相同的错误+1 – BonJon

+0

我不熟悉你的测试设置。你确定你正在''rootScope'注入mainCont和testCont文件吗? – tyler

+0

我会这么认为,我有另一个测试文件,可以用这样的设置工作 – BonJon

相关问题