当我尝试对我的 Controller 进行单元测试时,我收到错误。当我调试测试用例的期望时,我得到了预期值,但它因以下错误而失败。我在这里遗漏了什么,或者我是否在错误地测试 Controller 变量方式?
我的规范文件在下面
'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 中)引起的,然后代码试图将一个属性分配给未定义。因此修改 beforeEach 块如下
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
scope.page3 = {};
InformationController = $controller('InformationController', {
$scope: scope
});
}));
这解决了这个问题。
关于angularjs - karma 测试 : TypeError: Attempted to assign to readonly property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37696412/