2017-08-30 5 views
3

角度でいくつかの単体テストを行うためにNgbModalをモックする必要がありますが、これを行う方法はわかりません。これは私の関数である:角度NogbModalを模倣する

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

私が行うことになっていますか?

答えて

0

modalServiceNgbModalであると仮定すると、テストするロジックは、NgbModalではなく、モーダルコンテンツ(ProductModal)内にあるようです。

ご覧のとおり、.open()を使用した後、modalRef.componentInstanceProductModalのインスタンスになります。 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 
// …