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

标签 angularjs unit-testing karma-jasmine angularjs-controller

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

相关文章:

angularjs - Materialise Css - Materialzie 在选择选项时选择多个滚动

angularjs - D3 与 Angular 的集成 : "Error: Invalid value for <rect> attribute x"

javascript - 在 AngularJS 中保留路由更改的滚动位置?

java - 如何验证 JUnit 测试中抛出的异常的详细信息?

javascript - 测试 Redux 登录操作

angular - 如何使用 Angular 中的查询参数对 'navigate' 进行单元测试?

angularjs - 将 ES6 类与 Angular 服务/ Controller 一起使用时出错

android - Xamarin.Android 中的测试 Activity

rxjs - spy 方法返回错误 "method does not exist"

angular - 如果代码覆盖率不符合 Angular 6 中定义的阈值,则从 Karma 返回非零错误代码