leo*_*eog 5 unit-testing angularjs ng-switch angularjs-directive angularjs-scope
我正在尝试实现依赖于范围变量的测试.我想启用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');
}));
Run Code Online (Sandbox Code Playgroud)
我的问题是,我为此工作的实现没有得到范围变量"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 });
};
}
});
Run Code Online (Sandbox Code Playgroud)
有谁知道我做错了什么?任何帮助将不胜感激.
提前致谢!
UPDATE
只是为了澄清,这是一个如何从Angular团队测试ng-switch的例子.只是为了表明我正在以类似的方式进行测试,但没有达到预期的结果.
而且,我忘了将我的代码转换为$ rootScope,你到目前为止看到的是我尝试创建一个新范围以避免依赖$ rootScope进行更改.
it('should switch on value change', inject(function($rootScope, $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');
}));
Run Code Online (Sandbox Code Playgroud)
因此,在移动一些代码后,我发现应该在编译要测试的元素之前定义与更改 $rootScope 以测试功能无关的变量。感谢@Jonathan 的提示。
这是带有附加测试用例的工作代码:
it('should evaluate expressions to match switch value', inject(function($rootScope, $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');
}));
Run Code Online (Sandbox Code Playgroud)
谢谢大家。