Javascript 角度单元测试:this.router.myMethod不是函数
我正在进行单元测试,角度为4.0.0 我的测试用例如下所示:Javascript 角度单元测试:this.router.myMethod不是函数,javascript,angular,unit-testing,typescript,jasmine,Javascript,Angular,Unit Testing,Typescript,Jasmine,我正在进行单元测试,角度为4.0.0 我的测试用例如下所示: let mockRouter = { navigate: jasmine.createSpy('navigate') }; providers: [{provide: RouteNavigator, useValue: mockRouter}] test.spec.ts: // Test Suite of Que-Again features
let mockRouter = {
navigate: jasmine.createSpy('navigate')
};
providers: [{provide: RouteNavigator, useValue: mockRouter}]
test.spec.ts:
// Test Suite of Que-Again features
describe('QueueAgainComponent features', () => {
it('should navigate', () => {
comp.goToPrevious(); // HERE IS THE PROBLEM
expect(mockRouter.navigate).toHaveBeenCalledWith(['/home/pilote'], {skipLocationChange: true});
sharedclientService.user.isAdviser = true;
comp.goToPrevious();
expect(mockRouter.navigate).toHaveBeenCalledWith(['/home/advisor'], {skipLocationChange: true});
});
在配置部分下,我模拟了我的RouteNavigator服务,如下所示:
let mockRouter = {
navigate: jasmine.createSpy('navigate')
};
providers: [{provide: RouteNavigator, useValue: mockRouter}]
我的测试失败,但出现以下错误:
TypeError:this.router.myMethod不是函数
日志文件显示问题出现在98:18,这正是这一行:
comp.goToPrevious()
因此,goToPrevious()在我的组件中实现如下:
goToPrevious() {
this.routeNavigator.myMethod();
}
而routeNavigator指用于处理自定义重定向的自定义服务,如下所示:
**Route-navigator.service.ts**
import { LoginComponent } from '../../login/login.component';
import { SharedclientService } from '../../service/sharedclient.service';
import { Injectable } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
@Injectable()
export class RouteNavigator {
constructor(private router: Router, private sharedclientService: SharedclientService, private activatedRoute: ActivatedRoute) { }
accessAdvancedSearchFromRegistration = false;
accessSyntheseWhileGdfaShown = false;
track360View = false;
oldUrl: string;
private rdvReferer: string;
public navigateTo(url: string) {
this.oldUrl = this.router.url;
this.router.navigate([url], { skipLocationChange: true });
}
public myMethod() // HERE IS MY METHOD
{
if(this.sharedclientService.user != null && this.sharedclientService.user.isAdviser==null){
this.navigateTo('/home/blank');
this.sharedclientService.setCurrentRole('');
}
else if (this.sharedclientService.user != null && this.sharedclientService.user.isAdviser) {
this.navigateTo('/home/advisor');
this.sharedclientService.setCurrentRole('VENDEUR');
} else {
this.navigateTo('/home/pilote');
this.sharedclientService.setCurrentRole('PILOTE');
}
}
public goToNextAdvancedSearch() {
if (this.accessAdvancedSearchFromRegistration || !this.sharedclientService.user.isAdviser) {
this.navigateTo('/home/registration');
}
else {
this.track360View = true;
this.navigateTo('/home/view-360');
}
}
public exitAdvancedSearch() {
if (this.accessAdvancedSearchFromRegistration) {
this.navigateTo('/home/registration');
}
else {
this.goToHomeAccordingToProfile();
}
}
goToRdv(url?:string) {
if(!url)
this.rdvReferer = this.router.url;
else this.rdvReferer=url;
this.navigateTo('/home/appointment');
}
exitRdv() {
this.navigateTo(this.rdvReferer);
}
goToBlank() {
this.navigateTo('/home/blank');
}
public url() {
return this.router.url;
}
}
有没有关于问题是什么或如何处理的想法???因为在
goToPrevious()
中,您使用this.router.myMethod()代码>
除非你扩展了路由器,否则我在Angular的路由器模块中不知道这一点
编辑模拟应包含以下内容:
let mockRouter = {
navigate: jasmine.createSpy('navigate'),
myMethod: () => {/* mock it as you want here */}
};
providers: [{provide: RouteNavigator, useValue: mockRouter}]
因为在goToPrevious()
中使用this.router.myMethod()代码>
除非你扩展了路由器,否则我在Angular的路由器模块中不知道这一点
编辑模拟应包含以下内容:
let mockRouter = {
navigate: jasmine.createSpy('navigate'),
myMethod: () => {/* mock it as you want here */}
};
providers: [{provide: RouteNavigator, useValue: mockRouter}]
是的,它不是默认的路由器,它是一个扩展的路由器,我已经附加了它的实现。好的,那么你能告诉我你是如何在测试中模拟它的吗?我已经更新了我所有的问题,为你提供了更多细节;)我现在看到了。在模拟中,您声明了methode-navigate,但没有声明myMethod
!因为它没有申报,测试台不知道他在做什么。尝试实现它并查看下一个错误!我没想到;更多详细信息?是的,这不是默认路由器,它是一个扩展的路由器,我已经附加了它的实现。好的,那么你能告诉我你是如何在测试中模拟它的吗?我已经更新了我所有的问题,为你提供了更多细节;)我现在看到了。在模拟中,您声明了methode-navigate,但没有声明myMethod
!因为它没有申报,测试台不知道他在做什么。尝试实现它并查看下一个错误!我没想到;更多细节?