gpt4 book ai didi

angular - 在 Angular 中模拟 NgbModal

转载 作者:太空狗 更新时间:2023-10-29 17:21:01 25 4
gpt4 key购买 nike

我需要模拟一个 NgbModal 来做一些 Angular 单元测试,但我不知道该怎么做。这是我的功能:

    openModal(deviceID: string, productID: string){
const modalRef = this.modalService.open(ProductModal)
modalRef.componentInstance.content = {
deviceId: deviceID,
productId: productID
}
modalRef.componentInstance.toEmit.subscribe(($e) => {
if ($e === true) this.reloadList();
});
}

我该怎么办?

最佳答案

假设您的 modalServiceNgbModal ,您要测试的逻辑似乎在模态内容 ( ProductModal ) 中,而不是 NgbModal本身。

如你所见,使用.open()后你的modalRef.<em>component</em>Instance将是 ProductModal 的实例;所以你可以测试ProductModal作为任何组件,例如组件夹具并跳过 modalService一共:

(同样,假设 ProductModal 是一个具有适当装饰的组件。)

let component: ProductModal;
let fixture: ComponentFixture<ProductModal>;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ NgbModal, NgbActiveModal ],
declarations: [ ProductModal ]
});
fixture = TestBed.createComponent(ProductModal);
component = fixture.componentInstance;
fixture.detectChanges();
});

// now you have the component in `component`, so you can test
// component.content
// component.toEmit
// …

关于angular - 在 Angular 中模拟 NgbModal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45959001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com