2017-08-14 7 views
0

による経路の変化のためのユニットテストにはノーあります..下記 は私のhtmlコード..です2角度 - [routerLink]

<button md-raised-button class="mat-primary"[routerLink]="['add']"> 
<i class="fa fa-plus" aria-hidden="true"></i> ADD 
</button> 

を材料設計を使用して、経路変更のためのテストケースを記述する必要があるのですどのクラスもボタンに関連付けられており、オンクリック機能は記述されていません。 私はangular2で新しく、テストするための多くのオプションを試しましたが、この種のジャスミンユニットテストケースを作成するにはどうすればよいでしょうか?

+0

以下のように私たちは、「重複」とのタイトルを編集しないように、ルータのリンクコードが含まれている成分が含まモックコンポーネントを作成する必要があります。代わりに、近い票を使用してください。票がない場合は、次のようにコメントにリンクを投稿してください:[routerLinkを使った角度2ユニットテストコンポーネント](https://stackoverflow.com/questions/39577920/angular -2ユニットテストコンポーネントとルータリンク)質問を返す – crashmstr

答えて

-1

あなたは

Angular 2 unit testing components with routerLink

import { Component } from '@angular/core'; 
import { Router } from '@angular/router'; 
import { By } from '@angular/platform-browser'; 
import { Location, CommonModule } from '@angular/common'; 
import { RouterTestingModule } from '@angular/router/testing'; 
import { TestBed, inject, async } from '@angular/core/testing'; 

@Component({ 
    template: ` 
    <a routerLink="/settings/{{collName}}/edit/{{item._id}}">link</a> 
    <router-outlet></router-outlet> 
    ` 
}) 
class TestComponent { 
    collName = 'testing'; 
    item = { 
    _id: 1 
    }; 
} 

@Component({ 
    template: '' 
}) 
class DummyComponent { 
} 

describe('component: TestComponent', function() { 
    beforeEach(() => { 
    TestBed.configureTestingModule({ 
     imports: [ 
     CommonModule, 
     RouterTestingModule.withRoutes([ 
     { path: 'settings/:collection/edit/:item', component: DummyComponent } 
     ]) 
     ], 
     declarations: [ TestComponent, DummyComponent ] 
    }); 
    }); 

    it('should go to url', 
    async(inject([Router, Location], (router: Router, location: Location) => { 

    let fixture = TestBed.createComponent(TestComponent); 
    fixture.detectChanges(); 

    fixture.debugElement.query(By.css('a')).nativeElement.click(); 
    fixture.whenStable().then(() => { 
     expect(location.path()).toEqual('/settings/testing/edit/1'); 
     console.log('after expect'); 
    }); 
    }))); 
});