繁体   English   中英

在我的情况下如何测试http请求

[英]How to test a http request in my case

我正在尝试测试rootscope 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')

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM