TypeError: Cannot set property of undefined in angular scope

1.3k Views Asked by At

I'm running the test case in angular 1.5 version, but I'm getting below error even though I have correct logic in postToRegisterController controller and dealerNumber is working fine in application.

postToRegisterController
   scope.contract.dealNumber
     × set valid deal number 
TypeError: Cannot set property 'dealNumber' of undefined
   at <Jasmine>
   at UserContext.<anonymous> (src/assets/js/controllers/portal-integrated-apps/quikRegister/postToRegisterController.spec.js:46:43)

Given below is the piece of code from my Controller. (() => {

   describe('postToRegisterController', () => {
       let $ctrl;
       let $controller;
       let $state;
       let quikRegisterService;
       let scope;
       let $uibModalInstance;
       let $uibModal;
       let $rootScope;
       let services;

       beforeEach(module('app'));
       beforeEach(module('app.quikregister'));
       beforeEach(module('ui.bootstrap'));
       beforeEach(inject(function(_$state_, _quikRegisterService_, _$controller_,  _$rootScope_, _services_) {

           $controller = _$controller_;
           $state = _$state_;
           quikRegisterService = _quikRegisterService_;
           scope = _$rootScope_.$new();
           $rootScope = _$rootScope_;
           services = _services_;
           $uibModalInstance = jasmine.createSpyObj('$uibModalInstance', ['close', 'dismiss']);
           $uibModal = jasmine.createSpyObj('$uibModal', ['open']);

           // $uibModalInstance = $uibModalInstance;
           // $uibModal = $uibModal;
           initController();
       }));

       beforeEach(() => {
           spyOn(quikRegisterService, 'store');
          spyOn($state, 'go');
           $state.current.data = {
               breadcrumbs: [
                   'Search'
               ]
           };
           initController();
       });

       describe('scope.contract.dealNumber', function() {
           it('set valid deal number ', function() {
               scope.contract.dealNumber = 1234;
               scope.validateDealNumber();
               expect(scope.isValidDealNumber).toBe(true);
           });
       });

       function initController() {
           $ctrl = $controller('postToRegisterController', {
               quikRegisterService: quikRegisterService,
               $state: $state,
               $rootScope: $rootScope,
               $uibModalInstance: $uibModalInstance,
               $scope: scope,
               $uibModal: $uibModal
           });
           scope.$digest();
       }

   });

}) ();

Am i missing something? Any inputs appreciated. Please let me know what is wrong here.

2

There are 2 best solutions below

0
Bohdan Stupak On

Looking upon your code I can't find any reason why scope.contract should not be undefined indeed. So as a workaround you could try

scope.contract = {
   dealNumber = 1234
}
0
Gonçalo Castilho On

You have the scope variable instantiated in the scope = _$rootScope_.$new(); but you never give the scope.contract a value so it is undefined.

Since scope.contract is undefined then if you try to assign a value to scope.contract.dealNumber then it will always return an error saying you cant set deal number of undefined (which is the scope.contract in this case).

Just do

scope = _$rootScope_.$new();
scope.contract={};

and it should work.