我正在编写单元测试,以查看是否使用正确的参数调用了路由器链接。
component.html
<tr *ngFor="let record of records;let i = index">
<td>
<button [routerLink]="['/account-details', record['account']]" class="btn">Show Detail</button>
</td>
</tr>
component.spec.ts
import { RouterTestingModule } from '@angular/router/testing';
import { Router } from '@angular/router';
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, HttpClientTestingModule],
declarations: [ AccountsListComponent]
})
.compileComponents();
injector = getTestBed();
httpMock = injector.get(HttpTestingController);
injector.get(router);
}));
beforeEach(() => {
fixture = TestBed.createComponent(AccountsListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should fetch details for particular account on click event', () => {
component.records = [ {"account":""xyz"}];
fixture.detectChanges();
let componentLinks = fixture.debugElement.queryAll(By.css('.btn'));
componentLinks[0].triggerEventHandler('click',null);
fixture.detectChanges();
const spy = spyOn(router, 'navigate');
expect(spy).toHaveBeenCalledWith(['/account-details',"xyz"]);//fails
“未处理的承诺拒绝:”,“无法匹配任何路线。网址段:“ account-details / xyz”,“;区域:','ProxyZone',';任务:','Promise.then',';值:',错误:无法匹配任何路由。网址段:“ account-details / xyz” 错误:无法匹配任何路线。网址段:“ account-details / xyz”