Karma1.7 + AngularJS1.8.2 使用 $compile 方法导致无法配置的 属性 错误

Karma1.7 + AngularJS1.8.2 using $compile method causes unconfigurable property error

最近将 angularjs 应用程序从 1.5.11 更新到 1.8.2

这破坏了我们的几个单元测试。似乎都在抛出错误:

 TypeError: Attempting to configurable attribute of unconfigurable property.

经过一些调试,每当我们的指令规范之一调用 $compile()

时就会发生这种情况

这是指令规范进行的调用之一的副本:

    element = angular.element('<input id="labels" type="hidden" ui-select2="select2Options" ng-model="labels">');
    compiled = $compile(element)(scope);
    $(document.body).append(element);
    element.scope().$apply();

我查看了文档,发现与 $compile 中的更改有关的唯一参考与 the removal of the preAssignBindingsEnabled function 有关。好像在 1.6 之前,这是默认打开的。在 1.6 中它默认关闭,但如果您明确设置标志则支持。从 1.7 开始,您将无法再设置标志。

我不确定这是否是我的问题的原因,但通读文档后,我不确定此更改是否真的相关? 代码按预期编译和工作,只是测试出错了。非常感谢任何帮助!

这是一个快速解决方法,但可能有助于确定最终解决方案:

为了解决这个问题,我们将 angular-mocks-1.8.2.js 降级为 angular-mocks-1.6.4.js,因为它似乎出现在我们的单元测试链中使用 compileProvider.preAssignBindingsEnabled() 方法。

我们也将 Jasmin/Karma 升级到最新版本,但不确定这是否真的是解决方案的一部分。