R.S Molomo
R.S Molomo

Reputation: 51

Expected undefined to be defined How do i fix it

I am trying to test my Angular with Jasmine but somehow i keep getting this error Expected undefined to be defined and i have followed that angular documentation example i am on mean stack

test.js

describe('Testing Ecdreport Controllers', 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();
})

});

Upvotes: 4

Views: 14782

Answers (1)

Tom Shen
Tom Shen

Reputation: 1045

You get that error because your controller in test is never defined. You need to use var controller = ...

You should use controller injection like this :

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

and initialize the controller in each test like this :

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

This way you can also pass on different parameters in each test if your controller requires any data to be passed on to.

Upvotes: 1

Related Questions