4

I'm working on writing unit tests for my ionic 2 app, and I have a @ViewChild call that looks like this:

  <ion-slides #slides (ionSlideDidChange)="onSlideChanged()" pager>

and in my slides.ts I have this:

@ViewChild(Slides) slides: Slides;

This works fine when running the server, and the slides work great, but when I try and write a unit test, slides is undefined. This, of course, breaks everything else.

My unit test setup looks like this:

beforeEach(async(() => TestUtils.beforeEachCompiler([HomePage]).then(compiled => {
  fixture = compiled.fixture;
  component = compiled.instance;
  component.ngOnInit();
})));

it('should have a defined component', () => {
  // The next line is where it starts throwing the errors.
  fixture.detectChanges();
  expect(component).toBeDefined();
});

What can I do to get my @ViewChild defined correctly in a unit test?

slooker
  • 1,060
  • 2
  • 9
  • 16
  • Did you take a look at this? http://stackoverflow.com/questions/36439450/on-console-log-viewchild-variable-is-undefined-in-unit-test . The Inject async might be helpful – misha130 Feb 14 '17 at 18:59

0 Answers0