获取错误无法使用karmajs在angularjs的单元测试中读取null属性

问题描述

大家好,我是angularjs和单元测试的新手。我有以下HTML代码

<div ng-if="areUserLicensesAvailable()" tooltip="Add new employee" class="sv-button"></div>

这是我的areUserLicensesAvailable的angularjs代码

$scope.areUserLicensesAvailable =function () {
    if (my.PortalState.Account.selected.accountExProperties.available_user_licenses == 0) {
        return false;
    }
    else {
        return true;
    }
}

代码在前端完美运行,并且表现正常。但是它的测试不起作用,这里是测试代码

describe("areUserLicensesAvailable",function() {
            it("returns true when account has available user licenses",function() {
                my.PortalState = {
                            Account:
                            {
                                selected:
                                {
                                    accountExProperties:
                                    {
                                        available_user_licenses: 0
                                    }
                                }
                            }
                };
                expect(scope.areUserLicensesAvailable()).toBe(false);
            });
    });

我在调用scope.areUserLicensesAvailable()之前已经定义了我的对象,但是我收到此错误

TypeError:无法读取null的属性'accountExProperties' 在ChildScope.Controller。$ scope.areUserLicensesAvailable(src / _combined.js:9:503638) 在UserContext上。 (src / nodes / entity / firm-employee-list.tests.js:240:30)

我做错什么了吗?请帮忙

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)