2016-08-12 13 views
0

각도 2.0 구성 요소에 대한 일부 단위 테스트를 설정하려고합니다. Angular 2.0 웹 사이트의 설명서에는 Jasmine을 사용하는 일반 단위 테스트에 대한 정보가 있지만 구성 요소는 구체적으로 나와 있지 않지만 두 가지 기사 (예 : https://medium.com/google-developer-experts/angular-2-unit-testing-with-jasmine-defe20421584)를 찾았으며 종속성을 촉진하기 위해 "angular2/testing"을 사용하여 참조했습니다 주입 및 구성 요소 테스트. 하는 방법으로이 여전히 올바른 또는 선호 작품을 사용하는 경우 https://code.angularjs.org/2.0.0-beta.9/testing.dev.jsAngular 2.0에서는 구성 요소를 어떻게 테스트합니까?

사람이 알고 있나요 :

그러나, 나는이에 찾을 수있는 최신 참조 베타,보다 최근의 RC 버전이 아닌 하나입니다 Angular 2.0에서 구성 요소 테스트를 수행 하시겠습니까?

편집 : 명확히하기 위해, 나는 기능을 수행하는 구성 요소를 테스트하는 방법을 찾고있을 뿐이며이를 반영하기 위해 위의 질문을 업데이트했습니다.

편집 2 : TestComponentBuilder (angular.io/docs/ts/latest/api/core/testing/TestComponentBuilder-class.html)는 내가 원하는 대상에 적합하지만 더 이상 사용되지 않습니다. 각도와 2 rc.5

답변

0

이것을 시도 (이 예 TestBed 사용) :

import { provide } from '@angular/core'; 
import { async, TestBed } from '@angular/core/testing'; 

import { SomeComponent } from './some.component'; 

beforeEach(() => { 
    TestBed.configureTestingModule({ 
    declarations: [ 
     SomeComponent 
    ], 
    providers: [ 
     SomeComponent, 
     // ServiceA, 
     // provide(ServiceB, { useClass: TestServiceB }) 
    ], 
    imports: [ 
     // HttpModule, 
     // etc. 
    ] 
    }); 
}); 

it('should do something', async(() => { 
    // Overrides here, if you need them: 
    TestBed.overrideComponent(SomeComponent, { 
    set: { 
     template: '<div>Overridden template here</div>' 
     // ... 
    } 
    }); 

    TestBed.compileComponents().then(() => { 
    const fixture = TestBed.createComponent(SomeComponent); 

    // Access the dependency injected component instance: 
    const app = fixture.componentInstance; 

    // Access the element 
    const element = fixture.nativeElement; 

    // Detect changes to wire up the `fixture.nativeElement` as necessary: 
    fixture.detectChanges(); 

    expect(app.something).toBe('something'); 
    }); 
})); 
관련 문제