Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular TypeError:this.service.x不是一个函数_Angular_Unit Testing_Karma Jasmine - Fatal编程技术网

Angular TypeError:this.service.x不是一个函数

Angular TypeError:this.service.x不是一个函数,angular,unit-testing,karma-jasmine,Angular,Unit Testing,Karma Jasmine,我正在尝试为LoginComponent.onSubmit()编写一个测试用例。在这个方法中,组件调用this.authService.login(email,password),它返回一个字典{emailValid:boolean,passwordValid:boolean} 正在使用此服务的模拟版本。我通过console.log(this.authService)在LoginComponent.onSubmit()中检查了this.authService是否是MockAuthenticati

我正在尝试为
LoginComponent.onSubmit()
编写一个测试用例。在这个方法中,组件调用
this.authService.login(email,password)
,它返回一个字典
{emailValid:boolean,passwordValid:boolean}

正在使用此服务的模拟版本。我通过
console.log(this.authService)
LoginComponent.onSubmit()
中检查了
this.authService
是否是
MockAuthenticationService
的实例,它是。但是由于某些原因,它找不到登录方法

我尝试从
AuthenticationService
扩展
MockAuthenticationService
,然后覆盖登录方法,但该方法也不起作用

我真的不知道还能尝试什么

LoginComponent
spec文件:

class MockRoutingService{
页面:字符串;
构造函数(){}
路由(第页:字符串){
this.page=page;
}
}
类MockAuthenticationService扩展了AuthenticationService{
有效值:布尔值;
passwordValid:boolean;
登录(电子邮件:字符串,密码:字符串):{['emailValid']:布尔,['passwordValid']:布尔}{
返回{emailValid:this.emailValid,passwordValid:this.passwordValid};
}
}
描述('LoginComponent',()=>{
let组件:LoginComponent;
让authService:MockAuthenticationService;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[LoginComponent],
供应商:[
{提供:RoutingService,useClass:MockRoutingService},
{提供:AuthenticationService,useClass:MockAuthenticationService}
],
导入:[FormsModule]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(LoginComponent);
组件=fixture.componentInstance;
authService=TestBed.get(AuthenticationService);
fixture.detectChanges();
});
它('应该将emailErr设置为“*您的用户名不正确,请重试。“,()=>{
authService.emailValid=false;
authService.passwordValid=false;
setPassword('password');
setEmail('invalidemail.com');
onSubmit();
expect(component.emailErr).toBe(“*您的用户名不正确,请重试。”);
});
LoginComponent.onSubmit()

onSubmit(){
this.emailErr='';
this.passwordErr='';
常量响应:{['emailValid']:boolean,['passwordValid']:boolean}=this.authService.login(this.email,this.password);
const self=这个;
设置超时(()=>{
if(response.emailValid==false){
self.emailErr='*您的用户名不正确,请重试';
返回;
}else if(response.passwordValid==false){
self.passwordErr='*您的密码不正确,请重试';
返回;
}
self.routingService.route('home');
}, 300);
}
real
AuthenticationService.login()
方法:

login(电子邮件:string,密码:string):{['emailValid']:boolean,['passwordValid']:boolean}{
const responseBody=this.api.post('customers/authenticate',{email,password});
常量响应:{['emailValid']:boolean,['passwordValid']:boolean}={
emailValid:null,
passwordValid:null,
};
订阅(
(authData:any)=>{
response.emailValid=authData.emailValid==='true';
response.passwordValid=authData.passwordValid==='true';
if(response.passwordValid&&response.emailValid){
const currentCustomer=新客户();
assign(currentCustomer,JSON.parse(authData.customer));
setItem('currentCustomer',JSON.stringify(currentCustomer));
这个.customerService.GetCustomerReservations(currentCustomer).subscribe(
(reservationData:any)=>{
currentCustomer.reservations=reservationData.reservations;
}
);
this.currentCustomerSubject.next(currentCustomer);
}
返回;
});
返回响应;
}

你的值是一个类,而不是实例-使用
useClass
或创建一个实例。而且你的模拟真的是间谍。@jonrsharpe我已经试过了,但在发布这个问题时忘记了。我会更新帖子,顺便说一句,我只是再试了一次,不幸的是仍然没有运气。@jonrsharpe在你实际使用真实的s时不是间谍但是更改函数的返回值?或者你是说我应该使用spies?实际上MockRoutingService是一个间谍,MockAuthenticationService是一个存根,请参见,例如,你是否可以分叉并扩展我为你之前关于同一组件的问题创建的stackblitz以重新创建错误?设置看起来是正确的。你是s您是否仍会遇到此错误?不过,还有一个问题,您正在设置超时,因此需要在测试中使用
fakeAsync
,但这不会导致错误“方法loginService.x未定义”