简体   繁体   English

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

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

I am trying to test a rootscope http request in my case 我正在尝试测试rootscope http请求

I have something like 我有类似的东西

mainCont file mainCont文件

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

TestCont file TestCont文件

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

Test file 测试文件

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.  
        })
    })
});

I am not sure how to test the call in the testCont because when I run the test, I got an error saying 我不确定如何在testCont中测试呼叫,因为在运行测试时,出现错误提示

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

Can anyone help me to fix this issue? 谁能帮我解决此问题? Thanks a lot! 非常感谢!

You've defined testLoad as a function, so you need to call it by adding parentheses. 您已经将testLoad定义为一个函数,因此需要通过添加括号来调用它。 Change your code to 将您的代码更改为

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

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

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