범위 변수를 사용하는 테스트를 구현하려고합니다. ng-switch-when을 사용하여 표현식을 해결하고 싶습니다.AngularJS 범위 변수 테스트
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');
}));
내 문제는 내가이 작업을 수행하려면이 구현 범위 변수 "테스트"을 평가를하지 않는 이다 : 이것은 내가 (UPDATE에 $ rootScope 사용) 할 노력하고있어 것입니다 그리고 내가 기대하는대로 일한다. 구현은 다음과 같습니다.
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,이는 각 팀에서 테스트되고 어떻게 NG 스위치의 예입니다. 그냥 비슷한 방법으로 테스트를하고 있지만 예상 결과가 없다는 것을 보여 주기만하면됩니다.
또한 코드를 $ 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');
}));
시험 밖에서 작동합니까? 테스트가 실행될 때 element.text()는 무엇을 반환합니까? – UnicodeSnowman
예, 실제 사용 사례에서 작동합니다. 테스트가 실행되면 ** element.text() **는 ''(빈 문자열)입니다. 그것을 디버깅 한 결과, 내부 변수 컴파일시 범위 변수에 실제 사용 사례에서와 같이 "test"속성이없는 것으로 나타났습니다. 아마도 이것은 스코프 가시성과 관련이 있습니다. – LeoG
이것은 내 알 수 없음을 보여줄 수 있지만 테스트의 목적을 확인할 수 있습니까? ngSwitch의 AngularJS 소스를 게시 한 것 같습니다. 테스트 해 보시겠습니까? –