Unit testing useShadowDom的AngularDart单元测试:false
我正在尝试为useShadowDom设置为false的angular.dart(1.1.2)组件编写单元测试。如果useShadowDom设置为true,我似乎无法找到引用组件范围的方法Unit testing useShadowDom的AngularDart单元测试:false,unit-testing,dart,angular-dart,Unit Testing,Dart,Angular Dart,我正在尝试为useShadowDom设置为false的angular.dart(1.1.2)组件编写单元测试。如果useShadowDom设置为true,我似乎无法找到引用组件范围的方法 @Component( selector: 'test_component', templateUrl: 'test_component.html', useShadowDom: false ) class TestComponent { bool clicked = fals
@Component(
selector: 'test_component',
templateUrl: 'test_component.html',
useShadowDom: false
)
class TestComponent {
bool clicked = false;
clickHandler() {
clicked = true;
}
}
然后在我的测试中:
inject((TestBed tb) async {
Scope s = tb.rootScope.createChild({"clicked" : false});
Element el = tb.compile("<test_component></test_component>", scope: s);
microLeap();
await tb.rootScope.apply();
ShadowRoot root = await el.shadowRoot;
// When useShadowDom: true, then use root here instead
Element theButton = el.querySelector("#the_button");
theButton.click();
microLeap();
await testBed.rootScope.apply();
// If I do this with useShadowRoot: true, then it works perfect
// (true) but root is null with useShadowDom: false and if I pass
// element, then scope.context is just a map with the original
// values (clicked = false).
print((testBed.getScope(root).context as TestComponent).clicked);
// This just prints the original values (clicked = false)
await s.context.forEach((String key, Object val) async => print("$key : $val"));
});
您是刚开始使用Angular.dart还是希望引入单元测试的成熟项目?它是一个已有单元测试的现有项目(大约9个月)。我们在新的单元测试中添加了新的功能,但是遇到了这个问题。在这个具体的例子中,上面的工作让我们。。。但这似乎是测试床上的一个bug。您是刚开始使用Angular.dart还是希望引入单元测试的成熟项目?它是一个已有单元测试的现有项目(大约9个月前)。我们在新的单元测试中添加了新的功能,但是遇到了这个问题。在这个具体的例子中,上面的工作让我们。。。但这似乎是测试床上的一个bug。
dynamic getComponentContext(Node el, Function typeCallback, TestBed tb) {
if(el is Element && el.childNodes != null && el.childNodes.isNotEmpty) {
Scope s = tb.getScope(el);
if(typeCallback(s.context)) {
return s.context;
}
else {
for(Node childNode in el.childNodes) {
var c = getComponentContext(childNode, typeCallback, tb);
if(c != null) {
return c;
}
}
}
}
return null;
}