预期未定义要定义如何修复它

时间:2016-06-06 10:10:43

标签: jasmine karma-runner

我正在尝试用Jasmine测试我的Angular,但不知怎的,我不断得到这个错误预期未定义要定义,我已经遵循该角度文档示例我在平均堆栈

test.js

描述('测试Ecdreport控制器',function(){     var $ scope,controller;



var app = angular.module('mean.ecdreport',[])
.controller('EcdreportController', ['$scope', '$http', 'Global', 'Ecdreport', function($scope, $http, Global, Ecdreport) {
    $scope.global = Global;
    $scope.query = "";
    $scope.package = {
        name: 'ecdreport'
    };
    

            $scope.startDate = null;
            $scope.endDate = null;
            $scope.currentPage = 1;
            $scope.child= [];
            $scope.maxSize = 5;
            $scope.items = [];
            $scope.itemsPerPage = 10;
            $scope.totalItems = null;
            $scope.direction = 1;
            $scope.directionOld = 1;
            $scope.sortAttributes = 0;
            $scope.sortAttributesOld = 0;
            $scope.datamodel = null;
     

        $scope.getDataModel = function() {
           
            $http({url:'/api/v1/getdatamodel', method:"GET"})
                .success(function(data) {
                    console.log('Datamodel successful');
                    $scope.datamodel = data[0];
                    console.log('datamodel', data);
                   
                })
                .error(function(error) {
                    $scope.datamodel =[];
                });
        }
     //   console.log("Trying to get datamodel");
        $scope.getDataModel();
     });
                                    
                                    
                                    
   describe('Testing Ecdreport Controllers', function(){
	var $scope, controller;

	beforeEach(module('mean.ecdreport', function($controllerProvider){
 		$controllerProvider.register('EcdreportController', function(){
 		});
	}));
                                    
                                    
      beforeEach(inject(function(_$rootScope_,_$controller_){
			$scope = _$rootScope_.$new();
			controller = _$controller_('EcdreportController',
				{$scope : $scope
				});
		}));
	
	
	it('Should be registered', function(){
		expect(controller).toBeDefined();
	})

	it('Testing Scope', function(){
		expect($scope).toBeDefined()
		expect($Scope.getDataModel).toBeDefined();
	})

	
	
});                                 
                                    
                                    
                                    
   




beforeEach(module('mean.ecdreport', function($controllerProvider){
    $controllerProvider.register('EcdreportController', function(){
    });
}));


    beforeEach(inject(function(_$rootScope_,_$controller_){
        $scope = _$rootScope_.$new();
        controller = _$controller_('EcdreportController',
            {$scope : $scope
            });
    }));


it('Should be registered', function(){
    expect(controller).toBeDefined();
})

it('Testing Scope', function(){
    expect($scope).toBeDefined()

    expect($scope.getDataModel).toBeDefined();
})

});

1 个答案:

答案 0 :(得分:1)

您收到该错误,因为您的测试控制器从未定义过。您需要使用var controller = ...

您应该像这样使用控制器注入:

beforeEach(inject(function(_$rootScope_,_$controller_){
    $scope = _$rootScope_.$new();
    createController = function() {
        return _$controller_('EcdreportController', {
           $scope : $scope
        });
    };
}));

并在每个测试中初始化控制器,如下所示:

it('Should be registered', function(){
    var controller = new createController();
    expect(controller).toBeDefined();
})

这样,如果你的控制器需要传递任何数据,你也可以在每个测试中传递不同的参数。