Angular中的Mock NgbModal

前端之家收集整理的这篇文章主要介绍了Angular中的Mock NgbModal前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要模拟NgbModal以角度进行一些单元测试,但我不知道如何做到这一点.这是我的功能

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();
        });
    }

我应该做些什么?

解决方法

假设你的modalService是NgbModal,你想要测试的逻辑似乎是在模态内容(ProductModal)内,而不是NgbModal本身.

如您所见,在使用.open()后,您的modalRef.componentInstance将是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
// …

猜你在找的Angularjs相关文章