加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

单元测试 – 测试AngularJS范围变量

发布时间:2020-12-17 18:07:40 所属栏目:安全 来源:网络整理
导读:我正在尝试实现依赖于范围变量的测试.我想启用ng-switch-when来解析表达式.这就是我要做的事情(使用$rootScope更新): it('should switch on array changes',inject(function($rootScope,$compile) { element = $compile( 'div ng-switch="select"' + 'div n
我正在尝试实现依赖于范围变量的测试.我想启用ng-switch-when来解析表达式.这就是我要做的事情(使用$rootScope更新):

it('should switch on array changes',inject(function($rootScope,$compile) {
  element = $compile(
    '<div ng-switch="select">' +
      '<div ng-switch-when="test[0]">test[0]:{{test[0]}}</div>' +
    '</div>')($rootScope);
  expect(element.html()).toEqual('<!-- ngSwitchWhen: test[0] -->');
  $rootScope.test = ["leog"];
  $rootScope.select = "leog";
  $rootScope.$apply();
  expect(element.text()).toEqual('test[0]:leog');
}));

我的问题是,我为此工作的实现没有得到范围变量“test”来评估和工作,因为我期望.这是实施:

var ngSwitchWhenDirective = ngDirective({
  transclude: 'element',priority: 800,require: '^ngSwitch',compile: function(element,attrs) {
    return function(scope,element,attr,ctrl,$transclude) {
      var expr = scope.$eval(attrs.ngSwitchWhen),ngSwitchWhen = expr !== undefined ? expr : attrs.ngSwitchWhen;
      ctrl.cases['!' + ngSwitchWhen] = (ctrl.cases['!' + ngSwitchWhen] || []);
      ctrl.cases['!' + ngSwitchWhen].push({ transclude: $transclude,element: element });
    };
  }
});

有谁知道我做错了什么?任何帮助将不胜感激.

提前致谢!

UPDATE

只是为了澄清,这是一个如何从Angular团队测试ng-switch的例子.只是为了表明我正在以类似的方式进行测试,但没有达到预期的结果.

而且,我忘了将我的代码转换为$rootScope,你到目前为止看到的是我尝试创建一个新范围以避免依赖$rootScope进行更改.

it('should switch on value change',$compile) {
    element = $compile(
      '<div ng-switch="select">' +
      '<div ng-switch-when="1">first:{{name}}</div>' +
      '<div ng-switch-when="2">second:{{name}}</div>' +
      '<div ng-switch-when="true">true:{{name}}</div>' +
      '</div>')($rootScope);
    expect(element.html()).toEqual(
       '<!-- ngSwitchWhen: 1 --><!-- ngSwitchWhen: 2 --><!-- ngSwitchWhen: true -->');
    $rootScope.select = 1;
    $rootScope.$apply();
    expect(element.text()).toEqual('first:');
    $rootScope.name="shyam";
    $rootScope.$apply();
    expect(element.text()).toEqual('first:shyam');
    $rootScope.select = 2;
    $rootScope.$apply();
    expect(element.text()).toEqual('second:shyam');
    $rootScope.name = 'misko';
    $rootScope.$apply();
    expect(element.text()).toEqual('second:misko');
    $rootScope.select = true;
    $rootScope.$apply();
    expect(element.text()).toEqual('true:misko');
}));

解决方法

因此,在移动一些代码后,我发现在编译要测试的元素之前,应该定义在使用$rootScope更改以测试该功能时不涉及的变量.谢谢@Jonathan的暗示.

以下是包含其他测试用例的工作代码:

it('should evaluate expressions to match switch value',$compile) {
  $rootScope.array = ["leog",".me"];
  $rootScope.obj = {"key": "value"};
  $rootScope.$apply();
  element = $compile(
    '<div ng-switch="select">' +
      '<div ng-switch-when="obj.key">obj.key:{{obj.key}}</div>' +
      '<div ng-switch-when="array[0]">array[0]:{{array[0]}}</div>' +
      '<div ng-switch-when="array[1]">array[1]:{{array[1]}}</div>' +
    '</div>')($rootScope);
  expect(element.html()).toEqual('<!-- ngSwitchWhen: obj.key -->' +
                                 '<!-- ngSwitchWhen: array[0] -->' +
                                 '<!-- ngSwitchWhen: array[1] -->');
  $rootScope.select = "value1";
  $rootScope.$apply();
  expect(element.text()).toEqual('obj.key:value');
  $rootScope.select = "leog";
  $rootScope.$apply();
  expect(element.text()).toEqual('array[0]:leog');
  $rootScope.select = ".me";
  $rootScope.$apply();
  expect(element.text()).toEqual('array[1]:.me');
}));

谢谢大家.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读