3
角度のアクティブ化されたルートをスイートにスタブする方法を理解するのが苦労しています。私のコンポーネントの一つで、私は有効ルートのURLの一部を取得するためにルートのスナップショットを使用しています:角2のテストでのスタブアクティブ化
Error: Error in :0:0 caused by: undefined is not an object (evaluating 'this.route.snapshot.children[0].url')
だから私は:私のテストでは
let activatedPath = this.route.snapshot.children[0].url[0].path;
を、私はエラーを取得しています
class FakeActivatedRoute {
// stub detail goes here
}
と私のテストでそれを提供します:私は活性化したルートをスタブする必要がフィギュア
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [
SiteAdminComponent
],
schemas: [NO_ERRORS_SCHEMA],
providers: [
{ provide: ActivatedRoute, useClass: FakeActivatedRoute }
]
})
.compileComponents();
}));
私は誰でも.snapshot.children[0].url[0].path
に到達することができるスタブ実装に関するガイダンスを提供できますか?
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [
SiteAdminComponent
],
schemas: [NO_ERRORS_SCHEMA],
providers: [
{
provide: ActivatedRoute,
useValue: {snapshot: {children: [{url: ['your/path/here']}]}}
}
]
})
.compileComponents();
}));
あなたはこれと同じ階層にお答えします嘲笑activatedRouteオブジェクトを提供している。この方法:私は現在、あなたが好きな何かを行うことができるはず
[角度テストガイド](https://angular.io/docs/ts/latest/guide/testing.html)の「ActivatedRoute」を検索してください。スタブの例があります。しかし、私は自分で実装しようとしていますが、コンポーネントが作成される前にtestParamsを設定する方法がわかりません。多分お互いを助けることができるのでしょうか? :) – 27leaves
私は今_注射を_部分に持っています。これは、プロバイダの 'beforeEach'内で' activatedRoute = new ActivatedRouteStub(); 'と' {provide:ActivatedRoute、useValue:activatedRoute} 'と動作します。 私はスナップショットを使用しないので、この特定の問題ではお手伝いできませんが、これを出発点として、それほど難しいはずはありません;) – 27leaves
これに関するニュースはありますか?私もそれで苦労しています。 – Striped