Given a directive used for Dynamic Component Loading with ViewContainerRef injected:
import { Directive, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[fooHost]'
})
export class FooDirective {
constructor(public viewContainerRef: ViewContainerRef) {}
}
How would you inject an instance or mock of ViewContainerRef in a unit test:
import { FooDirective } from './foo.directive';
describe('FooDirective', () => {
it('should create an instance', () => {
const directive = new FooDirective();
expect(directive).toBeTruthy();
});
});
This most basic test fails due to the following error:
An argument for 'viewContainerRef' was not provided.
The testing guide does not cover this nor does there appear to be any testing module specifically for creating an instance of ViewContainerRef.
Is this is simple as creating a stub @Component with TestBed.createComponent and passing either the fixture or component instance as ViewContainerRef?
import { FooDirective } from './foo.directive';
import { ViewContainerRef, Component } from '@angular/core';
import { TestBed, async } from '@angular/core/testing';
@Component({ selector: 'app-stub', template: '' })
class StubComponent {}
describe('LightboxDirective', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({ declarations: [StubComponent] }).compileComponents();
}));
it('should create an instance', () => {
const fixture = TestBed.createComponent(StubComponent);
const component = fixture.debugElement.componentInstance;
const directive = new FooDirective(component);
expect(directive).toBeTruthy();
});
});
If that is the case, what should be passed as ViewContainerRef, the fixture.debugElement.componentInstance or fixture.debugElement.nativeElement or something else?
Thanks!