Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karma testing: TypeError: Attempted to assign to readonly property

Tags:

When I am trying to unit test my controller I am getting the error.When I debug the testcase's expect I am getting the expected value but its failing with below error.What am I missing here or whether I am testing the controller variable in a wrong manner?

My spec file is below

'use strict'; describe('Information.controller', function () { beforeEach(angular.mock.module('asp')); var InformationController, scope;  beforeEach(inject(function($controller, $rootScope) {     scope = $rootScope.$new();     InformationController = $controller('InformationController', {         $scope: scope     }); }));  fit('Should remove the object without info from the  object', function () {     InformationController.Data = [         {             "ban": "03745645455",             "accountNo": "0000drgyt456456565",             "id": "2c4cc5e8-f360367"         },         {             "ban": "27346fgh9080",             "accountNo": "000456456ytr655680",             "id": "2c4cc8ae-50bbf360367"         }     ];     InformationController.banList = InformationController.Data.map((value, index) => (value && value.ban ? {'id': index, 'text': value.ban} : null))     .filter(value => value);     expect(InformationController.banList.length).toBe(3); }); }); 
like image 495
Raphael Avatar asked Jun 08 '16 07:06

Raphael


1 Answers

readonly error is caused by scope.page(in my InformationController) being undefined and then the code is trying to assign a property to undefined.Hence the modified beforeEach block as below

beforeEach(inject(function ($controller, $rootScope) {         scope = $rootScope.$new();         scope.page3 = {};         InformationController = $controller('InformationController', {             $scope: scope         });     })); 

This solved the issue.

like image 136
Raphael Avatar answered Sep 19 '22 06:09

Raphael