我对角度测试真的很陌生,而且我找不到解决这个问题的方法。我试图对单击锚标记后调用的方法进行测试。这个锚标记位于一个 ng-container 中,它属于 ng-switch 的 case:
<ng-container [ngSwitch]="layout">
<ng-container *ngSwitchCase="'public'" >
<div class="d-flex>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarText"
>
</button>
</div>
<div class="class">
<ul class="class">
<ng-container *ngFor="let link of object">
<li class="nav-item">
<a
(click)="navigateToURL(link.route)"
>
{{ string }}
</a>
</li>
</ng-container>
</ul>
</div>
</ng-container>
<ng-container *ngSwitchCase="'other'">
<div class="class">
<ul class="class">
<ng-container *ngFor="let link of otherObject">
<li class="nav-item">
<a id='one'
(click)="navigateToURL(link.route)"
>
{{ other string }}
</a>
</li>
</ng-container>
</ul>
</div>
</ng-container>
</ng-container>
该方法将是某种路由器重定向
navigateToURL(url:stringstring):void{
this.router.navigateByURL(url)
}
然后在我的测试中
describe('AppComponent [navigation-root]', () => {
let fixture: ComponentFixture<AppComponent>;
let component: AppComponent;
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
providers: [
{
provide: Router,
useValue: routerMock,
},
],
schemas: [NO_ERRORS_SCHEMA],
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
})
})
);
test('Should create the navigation micro app', () => {
expect(component).toBeTruthy();
});
describe('When user is redirected from anchor tag click', () => {
test('Should call navigateToURL method from anchor tag on click ', () => {
const goToRouteSpy = jest.spyOn(component, 'navigateToURL');
const link = fixture.debugElement.nativeElement.querySelector('[id="one"]') as HTMLElement;
link.click();
fixture.detectChanges();
expect......
});
});
});
但是由于无法单击 null ,测试失败并引发错误有人可以帮我解决这个问题吗?非常感谢你