2016-06-08 64 views
19

當我試圖單元測試我的控制器時,我得到了錯誤。當我調試測試用例的期望時,我得到期望的值,但其失敗與以下error.What我在這裏失蹤或者我是否以錯誤的方式測試控制器變量?Karma測試:TypeError:嘗試分配到只讀屬性

我的規格文件低於

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

回答

26

只讀錯誤是由scope.page引起的(在我的InformationController)是不確定的,那麼代碼試圖分配財產undefined.Hence修改beforeEach塊作爲低於

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

這解決了這個問題。