Angular中的Mock NgbModal

Dar*_*ała 7 unit-testing jasmine ng-bootstrap ng-modal angular

我需要模拟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();
        });
    }
Run Code Online (Sandbox Code Playgroud)

我应该做些什么?

Jar*_*nen 1

假设您的modalServiceis NgbModal,您想要测试的逻辑似乎位于模态内容 ( ProductModal) 内,而不是NgbModal其本身。

\n\n

如您所见,使用后,.open()您的will 将成为;的实例。因此您可以使用例如组件夹具来测试任何组件 并跳过modalRef.componentInstanceProductModalProductModalmodalService

\n\n

(再次假设ProductModal 具有适当装饰的组件。)

\n\n
let component: ProductModal;\nlet fixture: ComponentFixture<ProductModal>;\nbeforeEach(() => {\n    TestBed.configureTestingModule({\n        providers: [ NgbModal, NgbActiveModal ],\n        declarations: [ ProductModal ]\n    });\n    fixture = TestBed.createComponent(ProductModal);\n    component = fixture.componentInstance;\n    fixture.detectChanges();\n});\n\n// now you have the component in `component`, so you can test\n// component.content\n// component.toEmit\n// \xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n