angular - ngOnDestroy 的茉莉花测试失败
问题描述
我正在尝试使用 jasmine 在 Angular 中编写测试 ngOnDestroy,并得到错误 Expected spy ngOnDestroy to have been called。有人可以告诉我问题是什么
零件
export class DialogComponent implements AfterViewInit, OnDestroy {
private readonly _onClose = new Subject<any>();
public componentRef: ComponentRef<any>;
public childComponentType: Type<any>;
public onClose = this._onClose.asObservable();
// add this:
@ViewChild(InsertionDirective, { static: false })
insertionPoint: InsertionDirective;
constructor(public componentFactoryResolver: ComponentFactoryResolver,
public cd: ChangeDetectorRef,
public dialog: DialogRef) {
}
ngAfterViewInit() {
this.loadChildComponent(this.childComponentType);
this.cd.detectChanges();
}
ngOnDestroy() {
if (this.componentRef) {
this.componentRef.destroy();
}
}
loadChildComponent(componentType: Type<any>) {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentType);
const viewContainerRef = this.insertionPoint.viewContainerRef;
viewContainerRef.clear();
this.componentRef = viewContainerRef.createComponent(componentFactory);
}
closeModal() {
this.dialog.close();
}
}
测试组件
describe('DialogComponent', () => {
let component: DialogComponent;
let fixture: ComponentFixture<DialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [SharedModule, DialogModule],
providers: [DialogConfig, DialogRef ]
})
.overrideModule(BrowserDynamicTestingModule, { set: { entryComponents: [DialogComponent, ExampleComponent] } })
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DialogComponent);
component = fixture.componentInstance;
component.childComponentType = ExampleComponent;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
fit('should call ngOnDestroy ', () => {
spyOn(component, 'ngOnDestroy').and.callThrough();
expect(component.ngOnDestroy).toHaveBeenCalled();
});
});
ComponentRef 定义
export declare abstract class ComponentRef<C> {
/**
* The host or anchor [element](guide/glossary#element) for this component instance.
*/
abstract readonly location: ElementRef;
/**
* The [dependency injector](guide/glossary#injector) for this component instance.
*/
abstract readonly injector: Injector;
/**
* This component instance.
*/
abstract readonly instance: C;
/**
* The [host view](guide/glossary#view-tree) defined by the template
* for this component instance.
*/
abstract readonly hostView: ViewRef;
/**
* The change detector for this component instance.
*/
abstract readonly changeDetectorRef: ChangeDetectorRef;
/**
* The type of this component (as created by a `ComponentFactory` class).
*/
abstract readonly componentType: Type<any>;
/**
* Destroys the component instance and all of the data structures associated with it.
*/
abstract destroy(): void;
/**
* A lifecycle hook that provides additional developer-defined cleanup
* functionality for the component.
* @param callback A handler function that cleans up developer-defined data
* associated with this component. Called when the `destroy()` method is invoked.
*/
abstract onDestroy(callback: Function): void;
}
解决方案
没有触发函数 ngOnDestroy 调用的事件。您应该做的是在您的测试中调用 ngOnDestroy,然后检查它是否执行所需的功能。
fit('should call ngOnDestroy ', () => {
let childFixture: ComponentFixture<ExampleComponent>;
component.componentRef = childFixture.componentRef;
spyOn(component.componentRef, 'destroy').and.callThrough();
component.ngOnDestroy();
expect(component.componentRef.destroy).toHaveBeenCalled();
});
推荐阅读
- javascript - 无法通过嵌套函数访问带有“this”的全局变量
- node.js - Docker compose extra_hosts 不会更改 /etc/hosts 文件
- c# - 如何使用隐藏字段在第二页中显示第一个表单数据?
- react-native - 纱线工作区中的 React Native 无法解析外部包
- python - Python中的通用对象是否有任何等效于np.unique的函数
- sql-server - 搜索数据库中字符串匹配的所有表并获取表结构
- java - 一组 OSGI 捆绑包的单独 GUI
- python - 如何以无头模式chrome python从网站上抓取实际数据
- python - 如何在列表中连续计算?
- c - 在 C 中全局打开文件描述符