角单元测试路由器链接状态

时间:2019-12-13 16:43:02

标签: angular unit-testing jasmine angular-router angular-routerlink

我使用state在角度(版本8)分量A和B之间传递数据,例如:

<label for="{{radio1.id}}" class="radio" (click)="radioSelected(radio1.label)">
    <input type="radio" id="{{radio1.id}}" name="optionsRadio41" value="{{radio1.value}}" required="" [disabled]="requestGroupName == 'SERVICE_GROUP'" [checked]="GroupName ==='ACCESS_GROUP'">
    <i class="skin"></i>
    <span>Within this solution</span>
</label>
&nbsp;&nbsp;&nbsp;&nbsp;
<label for="{{radio2.id}}" class="radio" (click)="radioSelected(radio2.label)">
    <input type="radio" id="{{radio2.id}}" [checked]="GroupName ==='SERVICE_GROUP'" name="optionsRadio41" value="{{radio2.value}}" required="">
    <i class="skin"></i>
    <span>Copy to a different solution</span>
</label>
<div *ngIf="radioSelectedLabel=='Within this solution'">first block </div>
<div *ngIf="radioSelectedLabel=='Within this solution'">second block</div>

在ComponentB中,我正在读取数据:

<a routerLink="/componentB" [state]="{data: {hello: 'world'}}">Go To ComponentB</a>

一切都可以在实时Web应用程序中运行,但是我似乎无法在茉莉花测试中复制相同的内容。 单元测试如下:

ngOnInit() {
  this.data = history.state.data.hello;
}

在将setup(); ... fixture.nativeElement.querySelector(`a`).click(); fixture.whenStable().then(() => { expect(fixture.nativeElement.querySelector(`p`)).toContain('world'); } 添加到this.data = ...之前,导航有效,但是现在存在空指针异常。实际上,ngOnInit()state,但我不知道为什么。实时运行webapp时一切正常,但是Jasmine单元测试失败。

我知道我可以添加null来进行设置,但是我希望在单元测试期间将真实数据从componentA传递到componentB。我想念什么?为什么一个组件中的数据不能通过history.pushState({data: {hello: 'world'}}, '', '');传输到另一个组件中?

0 个答案:

没有答案
相关问题