2016-08-12 7 views
0

私はAngular 2.0コンポーネントのユニットテストをセットアップしようとしています。 Angular 2.0 Webサイトのドキュメントには、Jasmineを使用した一般的な単体テストに関する情報がありますが、コンポーネント固有のものは何もありませんが、依存性を容易にするために「angular2/testing」を使用した2つの記事(例:https://medium.com/google-developer-experts/angular-2-unit-testing-with-jasmine-defe20421584)注射および成分試験。方法として、このまだ 正しいか好ま 作品を使用している場合https://code.angularjs.org/2.0.0-beta.9/testing.dev.jsAngular 2.0ではどのようにコンポーネントをテストしますか?

は誰もが知っています:

しかし、私はこれを見つけることができる最新のリファレンスは、ベータ、より最近のRC版のない1のためであります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'); 
    }); 
})); 
関連する問題