gpt4 book ai didi

angularjs - karma 测试 : TypeError: Attempted to assign to readonly property

转载 作者:行者123 更新时间:2023-12-03 10:58:39 27 4
gpt4 key购买 nike

当我尝试对我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com