【问题标题】:Testing enums outside the Controllers AngularJS在控制器 AngularJS 之外测试枚举
【发布时间】:2017-10-10 04:50:13
【问题描述】:

我的控制器中有一个枚举,但我无法使用 vm.MyEnum 从测试规范内部访问此枚举,因为枚举是在控制器类之外定义的。

export enum EditType {
  NO_EDITED = 0, ANNOTATED = 1, SIGNED = 2
}
export class MyCtrl extends AppCtrl {
  .......
}

我不能在我的测试中创建一个全局变量,因为它说我不能在严格模式下这样做。我如何在我的“描述”中得到它。谢谢你:)

【问题讨论】:

  • 您如何在测试规范中导入MyCtrlEditType
  • ` beforeEach(inject(function ($controller) { controller = $controller; })); . . . beforeEach(function() { vm = controller("MyCtrl", {$scope: scope }); });`

标签: javascript angularjs unit-testing typescript enums


【解决方案1】:

其实我很容易就解决了。在启动控制器之前,我只需要在beforeEachfunction 中插入不在控制器范围内的变量。 (我正在使用 SystemJS 模块加载器。其他模块加载器可能有不同的配置)

beforeEach(function() {
  EditType = System.registry.get(System.normalizeSync('app/pdfeditor/pdfviewer.controller')).EditType;
});

然后我开始使用另一个beforeEach 注入东西

beforeEach(inject(function(_$controller_) {
  controller = _$controller_;
}));
beforeEach(function() {
  vm = controller("MyCtrl", {
    $scope: scope
  });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多