2017-08-04 71 views
1

我有一個模式,只要用戶選擇頁面的菜單選項,即可打開。我遇到Isue的是,我對如何得到它打電話給我的函數調用在我ngOnInitAngular 2 - 茉莉花測試 - 打開模式在頁面加載

test.component.ts不知道

constructor(public dialog: MdDialog) { } 

ngOnInit() { 
    this.openTestModal(); 
} 

openTestModal() { 
    this.dialog.open(TestModalComponent, { 
     disableClose: true, 
     width: '600px' 
    }); 
} 

我已導入我的模型組件,並試圖:

變化password.component.spec.ts

import { TestModalComponent } from '../test-modal/test-modal.component'; 
    spyOn(component, 'openTestModal'); 
    spyOn(component, 'ngOnInit').and.callThrough(); 

    it('should be created',() => { 
     expect(component).toBeTruthy(); 
    }); 

錯誤

找不到TestComponent的組件工廠。你把它添加到 @ NgModule.entryComponents?

但它已經在那裏

回答

1

爲了解決這個問題您TestComponent在與entryComponents測試結束添加到模塊。

@NgModule({ 
    declarations: [TestComponent], 
    entryComponents: [ 
    TestComponent, 
    ], 
}) 
class TestModule {} 

並將TestModule添加到testBed導入。