С учетом директивы, используемой для Динамической загрузки компонентов с ViewContainerRef , введенной:
import { Directive, ViewContainerRef } from '@angular/core';
@Directive({
selector: '[fooHost]'
})
export class FooDirective {
constructor(public viewContainerRef: ViewContainerRef) {}
}
Как бы вы внедрили экземпляр или макет ViewContainerRef
в модульном тесте:
import { FooDirective } from './foo.directive';
describe('FooDirective', () => {
it('should create an instance', () => {
const directive = new FooDirective();
expect(directive).toBeTruthy();
});
});
Этот самый базовый тест не пройден из-за следующей ошибки:
Аргумент для viewContainerRef не предоставлен.
В руководстве по тестированию это не рассматривается, и, похоже, не существует какого-либо модуля тестирования, специально предназначенного для создания экземпляра ViewContainerRef
.
Это так просто, как создать заглушку @Component
с TestBed.createComponent
и передать экземпляр прибора или компонента как 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();
});
});
Если это так, что должно быть передано как ViewContainerRef
, fixture.debugElement.componentInstance
или fixture.debugElement.nativeElement
или что-то еще?
Спасибо!