Karma testing: TypeError: Attempted to assign to readonly property
Karma testing: TypeError: Attempted to assign to readonly property
当我试图对我的控制器进行单元测试时,我得到了 error.When 我调试了测试用例的预期,我得到了预期的值,但是它失败了,低于 error.What 我是不是错过了这里或者是否我以错误的方式测试控制器变量?
我的规格文件如下
'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);
});
});
只读错误是由 scope.page(在我的 InformationController 中)未定义引起的,然后代码试图将 属性 分配给 undefined.Hence 修改后的 beforeEach 块,如下所示
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
scope.page3 = {};
InformationController = $controller('InformationController', {
$scope: scope
});
}));
这解决了问题。
当我试图对我的控制器进行单元测试时,我得到了 error.When 我调试了测试用例的预期,我得到了预期的值,但是它失败了,低于 error.What 我是不是错过了这里或者是否我以错误的方式测试控制器变量?
我的规格文件如下
'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);
});
});
只读错误是由 scope.page(在我的 InformationController 中)未定义引起的,然后代码试图将 属性 分配给 undefined.Hence 修改后的 beforeEach 块,如下所示
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
scope.page3 = {};
InformationController = $controller('InformationController', {
$scope: scope
});
}));
这解决了问题。