我正在通过使用 toHaveBeenCalledWith
匹配它的某些部分来测试 url,参数应该没有 null/
。
expect(router.navigate).toHaveBeenCalledWith(jasmine.objectContaining(['/home']));
在我尝试上面的命令后,我得到了这个错误:
Expected spy navigate to have been called with [ <jasmine.objectContaining([ '/home' ])> ] but actual calls were [ [ 'null/home' ] ].
最佳答案
您需要使用 RouterTestingModule
设置您的测试,但这需要您测试导航结果而不是检查使用的导航参数。
相反,你可以有这样的东西:
class RouterMock {
navigate = jasmine.createSpy('navigate')
}
TestBed.configureTestingModule({
providers: [
{
provide: Router,
useClass: RouterMock
},
...
]
});
你可以像这样检查 spy 最后调用的参数:
it('should...', inject([Router], (router: RouterMock) => {
// ... setup test
expect(router.navigate.calls.mostRecent().args).toEqual(jasmine.objectContaining(['/home']));
}))
关于angular - 如何在 Angular 中匹配 toHaveBeenCalledWith 中的部分 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57507971/