19

Wenn ich versuche, Unit-Test mein Controller bekomme ich den Fehler.Wenn ich debuggen die Testfall erwarten, dass ich den erwarteten Wert, aber seine Fehler mit unter error.What erhalten Fehlt mir hier oder ob ich die Controller-Variable falsch teste?Karma-Test: TypeError: Versuch, readonly -Eigenschaft zuzuweisen

Meine Spec-Datei ist unter

'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); 
}); 
}); 

Antwort

26

Nur-Lese-Fehler durch scope.page verursacht wird (in meinem InformationController) undefiniert und dann wird der Code versucht, eine Eigenschaft zuweisen den modifizierten Block zu before undefined.Hence als unter

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

Dies löste das Problem.