如何使用angularjs中的karma单元测试来检查包含对象数组的范围变量?

时间:2017-04-24 06:45:16

标签: angularjs karma-jasmine

我有一个范围变量

$scope.FacilityLocations = [
{ Id: 1, Name: 'All', value: 'all', Selected: true },
{ Id: 2, Name: 'Facility Location 1', value: 'facilityLoc_1', Selected: true },
{ Id: 3, Name: 'Facility Location 2', value: 'facilityLoc_2', Selected: false },
{ Id: 4, Name: 'Facility Location 3', value: 'facilityLoc_3',  Selected: false }];

我的测试代码是......

it('Checking array of objects:',function() {
        expect($scope.FacilityLocations).toEqual([{ Id: 1, Name: 'All', value: 'all', Selected: true }, { Id: 2, Name: 'Facility Location 1', value: 'facilityLoc_1', Selected: true }, { Id: 3, Name: 'Facility Location 2', value: 'facilityLoc_2', Selected: false }, { Id: 4, Name: 'Facility Location 3', value: 'facilityLoc_3',  Selected: false }]);
    });

请建议我解决这个问题,我在这里错过了什么。

提前致谢。

它是this issue

的副本

1 个答案:

答案 0 :(得分:0)

//你必须注入控制器并定义范围

beforeEach(inject(function($controller,$rootScope,_$httpBackend_,_$http_){

           controller = $controller('Ctrl', {
                        $scope: scope
                                    });
           }));

//在"中使用范围代替$ scope;"功能