i have maincomponent
uses childcomponenta
@viewchild
. maincomponent
calling method on childcomponenta
.
i want write unit test case mocking childcomponenta
. how can using testbed
(in angular 2 rc5)?
before used use overridedirective(maincomponentname, childcomponenta, mockchildcomponenta);
there equivalent using testbed
?
i tried using
testbed.overridecomponent(childcomponenta,{ set: { template: '<div></div>' } });
which sets template, want mock methods in component well.
i think in case can try , replace child component mock component. create mock component same selector , use testbed remove declaration of real child component , add declaration mock component.
@component({ selector: 'child', template: '<div></div>' }) class mockcomponent { }
and in test use mock component:
testbed.configuretestingmodule({ imports: [mymodule], declarations: [parentcomponent, mockcomponent] }); testbed.overridemodule(mymodule, { remove: { declarations: [parentcomponent, childcomponent], exports: [parentcomponent, childcomponent] } });
more details here: https://github.com/angular/angular/issues/10689. make sure re-declare parentcomponent, not work otherwise (not sure why).
if use @viewchild reference child component, need modify not use type of component, id. use @viewchild('child') instead of @viewchild(childcomponent). see second example here: http://learnangular2.com/viewchild/
Comments
Post a Comment