title: Testing ViewContainerRef in angular6
body: <p>Hi I adding component dynamically using <code>viewContainerRef</code>.</p>
  
  <pre><code>export class DynamicComponentHolderComponent implements OnInit {
  
    comp: CompItem;
    @ViewChild(CompDirective) compHost: CompDirective;
  
    customUiList: Array&lt;CompItem&gt; = [];
  
    componentRef: any;
  
    constructor(
      public dialog: MatDialogRef&lt;DynamicComponentHolderComponent&gt;,
      @Inject(MAT_DIALOG_DATA) public receivedData: any,
      private componentFactoryResolver: ComponentFactoryResolver,
      private _componentService: CompService
    ) {
    }
  
    ngOnInit() {
      console.log(JSON.stringify(this.receivedData));
      this.getCustomComponentList();
    }
  
    loadComponent() {
      const dynamicItem = this.comp;
  
      const componentFactory = this.componentFactoryResolver.resolveComponentFactory(dynamicItem.component);
  
      const viewContainerRef = this.compHost.viewContainerRef;
      viewContainerRef.clear();
  
      this.componentRef = viewContainerRef.createComponent(componentFactory);
      (&lt;CompComponent&gt;this.componentRef.instance).data = this.receivedData.data;
  
  
      &lt;CompComponent&gt;this.componentRef.instance.
      updateEmitter.subscribe((response) =&gt; {
      this.dialog.close({ confirm: true, orderid: response.orderId, orderNumber: response.orderNumber });
      });
  
      // Default Close Emitter
      &lt;CompComponent&gt;this.componentRef.instance.
        closeEmitter.subscribe((response) =&gt; {
          this.closeDialog('close');
        });
  
    }
  
    getCustomComponentList() {
      this._componentService.getComponents()
        .subscribe(
          (response) =&gt; {
            if (response.length &gt; 0) {
              response.forEach((comp) =&gt; {
                this.customUiList.push(comp);
              });
              const classNameIndex = _.findIndex(this.customUiList, (o) =&gt; {
                return o.data.displayName == this.receivedData.data.customComponent;
              });
              console.log('FROM COMP', this.customUiList);
              if (classNameIndex &gt; -1) {
                this.comp = this.customUiList[classNameIndex];
                this.loadComponent();
              }
            }
          }
        );
    }
  
    ngOnDestroy() {
    }
  
    closeDialog(param: any) {
      &lt;CompComponent&gt;this.componentRef.destroy();
      this.dialog.close({ confirm: false });
    }
  
  
  }
  </code></pre>
  
  <p>Now i want to test, <code>this.componentRef.destroy();</code>. How can I do that in my spec.ts file. I have not idea how to do that. Need some help.</p>