2017-07-26 34 views
1

我正在使用Tab控件使用thoughtram博客文章。這裏是plnkr爲相同。對組件4中的組件進行單元測試

我想爲內部使用Tab組件的Tabs組件創建一個單元測試用例。不知道如何在Angular 4Jasmine做到這一點。

我如何在選項卡組件中注入Tab以便我可以覆蓋其ngAfterContentInit()selectTab()方法?

謝謝..

+0

我不知道這個線程是否有幫助。讓我知道..https://stackoverflow.com/questions/35975879/angular2-test-how-do-i-mock-sub-component – JGFMK

+0

是否有一個原因,你沒有單獨測試'Tab'組件到'Tabs'組件?我認爲這將是更好的選擇 – 0mpurdy

+0

爲了測試'Tabs'組件,你還必須傳遞'Tab'集合。這就是我的問題;我如何將它傳遞給'Tabs'組件。 – user7890278

回答

1

我將單元測試tabs通過包裝成一個測試組件和運行斷言上,如下圖所示:

@Component({ 
    template: ` 
    <tabs> 
     <tab title="tab-1"></tab> 
     <tab title="tab-2"></tab> 
    </tabs>`, 
}) 
class TestTabsComponent { } 


describe("Component: Tabs",() => { 
    let component: TestTabsComponent; 
    let fixture: ComponentFixture<TestTabsComponent>; 

    beforeEach(() => { 
    TestBed 
     .configureTestingModule({ 
     declarations: [ 
      TabsComponent, 
      TabComponent, 
      TestTabsComponent, 
     ], 
     }); 

    fixture = TestBed.createComponent(TestTabsComponent); 
    component = fixture.componentInstance; 
    }); 

    it('should have tab title', async(() => { 
    fixture.detectChanges(); 
    let compiled = fixture.debugElement.queryAll(By.css('tab')); 
    expect(compiled[0].nativeElement.title).toBe('tab-1'); 
    expect(compiled[1].nativeElement.title).toBe('tab-2'); 
    })); 

    afterEach(() => { 
    fixture.destroy(); 
    }); 
}); 

希望這將有助於!

+0

謝謝..我在此之後得到了100%的覆蓋率。 – user7890278