angular - 如何为行为主题编写单元测试
问题描述
@Input() public openDrawer: BehaviorSubject<{
open: boolean;
assetConditionDetails: AssetConditionIdDetails[];
selectedAssets: SelectedAssets[];
}>;
public ngOnInit(): void {
this.openDrawer.subscribe((result) => {
if (result) {
this.showLoader = result.open;
this.isDrawerActive = result.open;
this.selectedAssets = result.selectedAssets;
this.assetConditionDetails = result.assetConditionDetails;
}
});
}
有人可以告诉我如何为此编写单元测试用例..吗?这是我写的,但它说“失败:无法读取未定义的属性'订阅'”
it('should get users', async(() => {
component.openDrawer.subscribe(result => {
fixture.detectChanges()
expect(result.open).toBe(component.showLoader)
})
}))
解决方案
试试这个:
it('should get users', () => {
// mock openDrawer
component.openDrawer = new BehaviorSubject<any>({ open: true, assetConditionDetails: [], selectedAssets: [] });
// explicitly call ngOnInit
component.ngOnInit();
expect(component.showLoader).toBe(true);
expect(component.isDrawerActive).toBe(true);
expect(component.selectedAssets).toEqual([]);
expect(component.assetConditionDetails).toEqual([]);
});
推荐阅读
- php - 如何在终端中显示抓取的代码
- svg - 如何在 PHP 中使用 imageMagick 从 svg 路径数组中绘制图像
- javascript - 如何检查其是否为无效用户
- java - System.in 是如何工作的?
- javascript - 使用 Vue js axios 发布 api 调用不起作用
- python - 以交互方式将 python matplotlib 嵌入到我的演示文稿中的可能方法
- json - 在颤振中使用built_value创建不可变模型时出错
- php - 在 Woocommerce 中基于地理 IP 国家禁用除 BACS 之外的所有支付网关
- reactjs - 在 Jenkins 中运行 Jests 测试时无法从 index.js 中找到模块“joi”
- c - 在 C 中比较字符串的最快方法