代码之家  ›  专栏  ›  技术社区  ›  Umair Sarfraz

Jasmine angle unit test“无法读取未定义的”property“”

  •  2
  • Umair Sarfraz  · 技术社区  · 9 年前

    我刚刚开始学习角度单元测试。但是,对具有http调用的函数的测试失败。我已经指出了问题,但我无法解决。我知道这是个简单的问题

    控制器

    //Get data from URL
    vm.getJson = function() {
        var url = 'https://www.reddit.com/r/worldnews/new.json',
            count = 0;
        $http.get(url).success(function(response) {
            console.log(response);
            for (var i = 0; i < response.data.children.length; i++) {
                vm.data.push(response.data.children[i].data);
                count++;
                if (count === response.data.children.length) {
                    vm.numberOfPages();
                }
            }
    
            vm.result = true;
    
        }).error(function(err) {
            console.log(err);
        });
    
    };
    

    我得到的回应是: enter image description here

    规格

     //Testing the getJson function
    describe('vm.getJson()', function() {
    
       it('It should return dummy Data as response and vm.result to be truthy', function() {
    
        var dummyData = {name: 'Umair'};
        $httpBackend.whenRoute('GET','https://www.reddit.com/r/worldnews/new.json').respond(200, dummyData);
    
        MainCtrl.getJson(); 
    
        $httpBackend.flush();
    
        expect(MainCtrl.result).toBeTruthy();
    
    
    }); });
    

    如果我从控制器函数中删除循环,我不会得到任何错误,测试也会通过。我得到的错误是:

    无法读取未定义的“子级” 。从我随响应数据一起附上的图像中,children是数组。

    4 回复  |  直到 9 年前
        1
  •  3
  •   lastoneisbearfood    9 年前

    当您的测试运行时,$httpBackend实际上会截取 $http.get 呼叫并分配 dummyData 根据您在

    $httpBackend.whenRoute('GET','https://www.reddit.com/r/worldnews/new.json').respond(200, dummyData);
    

    这种模拟行为允许您快速完成单元测试,而不依赖于从测试机器可以访问reddit。所以在控制器中, response.data = {name: 'Umair'} 并且该对象没有名为的子对象 children .

    为了解决这个问题 dummy数据 ,尝试更多地模拟真实数据。

        2
  •  1
  •   Robin-Hoodie    9 年前

    您正在返回一个具有属性的对象 name 在您的测试中,然后您尝试访问该属性 data 这是未定义的。

    您应该在测试中模拟真实的响应对象,例如:

    var dummyData = {
      data: {
        children: [ 
        { data: 'foo'}
        ]
      }
    };
    
        3
  •  0
  •   Muthukannan Kanniappan    9 年前

    您dummyData不是数组,我想这可以解决问题,请尝试以下测试

    //Testing the getJson function
    describe('vm.getJson()', function() {
    
        it('It should return dummy Data as response and vm.result to be truthy', function() {
    
            var dummyData = [{ data: 'Umair' }];
            $httpBackend
                .when('GET', 'https://www.reddit.com/r/worldnews/new.json')
                .respond(.respond(
                    function() {
                        return [200, dummyData, {}];
                    }););
    
            MainCtrl.getJson();
    
            $httpBackend.flush();
    
            expect(MainCtrl.result).toBeTruthy();
    
        });
    });
    
        4
  •  0
  •   redrom    5 年前

    您应该在组件范围内定义未定义的变量:

     beforeEach(async () => {
        fixture = TestBed.createComponent(ExportWizardComponent);
        component = fixture.componentInstance;
        // DEFINE VALUES FOR VARIABLES
        component.patientInfo =  Constants.PROJECT_WIZARD_INFO;
        component.wizardMove = of(null);
    
        fixture.detectChanges();
      });
    
      it('should create', async () => {
        expect(component).toBeTruthy();
      });