【问题标题】:Karma testing: TypeError: Attempted to assign to readonly property业力测试:TypeError:试图分配给只读属性
【发布时间】:2016-10-08 08:41:49
【问题描述】:

当我尝试对我的控制器进行单元测试时,我收到了错误。当我调试测试用例的期望时,我得到了预期的值,但它失败并出现以下错误。我在这里缺少什么或者我是否正在测试控制器变量以错误的方式?

我的规范文件在下面

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

【问题讨论】:

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


    【解决方案1】:

    readonly 错误是由 scope.page(in my InformationController) 未定义引起的,然后代码试图将属性分配给未定义。因此修改 beforeEach 块如下

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

    这解决了问题。

    【讨论】:

      猜你喜欢
      • 2016-07-19
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多