2017-07-26 35 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