如何让 ViewChild 在单元测试中工作
How to make ViewChild work in a unit test
我有一个测试像
@Component(
selector: 'my-test',
template: 'none',
changeDetection: ChangeDetectionStrategy.OnPush,
)
class MyTestComponent {
final AngularClientCache clientCache;
MyTestComponent(this.clientCache) {
assert(clientCache != null);
}
}
@Component(
selector: 'test',
directives: const <dynamic>[MyTestComponent],
template: r'<my-test></my-test>',
)
class ComplexTestComponent {
@ViewChild(MyTestComponent) MyTestComponent testComponent;
}
@AngularEntrypoint()
void main() {
tearDown(() => disposeAnyRunningTest());
group('AngularClientCache', () {
test('should store and return value', () async {
final testBed =
new NgTestBed<ComplexTestComponent>().addProviders(<dynamic>[
provide(AngularClientCache, useValue: new AngularClientCache())
]);
final fixture = await testBed.create();
// Create an instance of the in-browser page loader that uses our fixture.
final loader = new HtmlPageLoader(
fixture.rootElement.parent,
executeSyncedFn: (c) async {
await c();
return fixture.update;
},
useShadowDom: false,
);
final complex =
await loader.getInstance<ComplexTestComponent>(ComplexTestComponent);
print(complex.testComponent); // <<<=== print `null`
});
});
}
如何获得对 MyTestComponent
组件的引用。
我预计 @ViewChild()
会这样做,但事实并非如此。也欢迎任何其他方法。
Matan Lurey 在 (https://github.com/dart-lang/angular_test/issues/50#issuecomment-291044761 中解释过)
resolvePageObject
is only for getting a package:pageloader
object.
What you're trying to do, I think:
await fixture.update((c) {
expect(c.testComponent, isNotNull);
});
我有一个测试像
@Component(
selector: 'my-test',
template: 'none',
changeDetection: ChangeDetectionStrategy.OnPush,
)
class MyTestComponent {
final AngularClientCache clientCache;
MyTestComponent(this.clientCache) {
assert(clientCache != null);
}
}
@Component(
selector: 'test',
directives: const <dynamic>[MyTestComponent],
template: r'<my-test></my-test>',
)
class ComplexTestComponent {
@ViewChild(MyTestComponent) MyTestComponent testComponent;
}
@AngularEntrypoint()
void main() {
tearDown(() => disposeAnyRunningTest());
group('AngularClientCache', () {
test('should store and return value', () async {
final testBed =
new NgTestBed<ComplexTestComponent>().addProviders(<dynamic>[
provide(AngularClientCache, useValue: new AngularClientCache())
]);
final fixture = await testBed.create();
// Create an instance of the in-browser page loader that uses our fixture.
final loader = new HtmlPageLoader(
fixture.rootElement.parent,
executeSyncedFn: (c) async {
await c();
return fixture.update;
},
useShadowDom: false,
);
final complex =
await loader.getInstance<ComplexTestComponent>(ComplexTestComponent);
print(complex.testComponent); // <<<=== print `null`
});
});
}
如何获得对 MyTestComponent
组件的引用。
我预计 @ViewChild()
会这样做,但事实并非如此。也欢迎任何其他方法。
Matan Lurey 在 (https://github.com/dart-lang/angular_test/issues/50#issuecomment-291044761 中解释过)
resolvePageObject
is only for getting apackage:pageloader
object.What you're trying to do, I think:
await fixture.update((c) { expect(c.testComponent, isNotNull); });