this.mobilityService.currentMessage.subscribe 不是 angular 单元测试中的函数
this.mobilityService.currentMessage.subscribe is not a function in angular unit testing
在 karma 单元测试中我得到 this.mobilityService.currentMessage.subscribe is not a function 错误
下面是我试图通过服务从另一个组件获取值的 .ts 文件
constructor(private projectService: ProjectService, private teamService: TeamserviceService) {this.getData()}
getData(){
this.projectService.currentMessage.subscribe(data => {
if(data!=null)
{
this.serviceName=data.service_object.serviceName;
this.serviceId = data.service_object.id})
这是我的服务代码的样子 -
messageSource = new BehaviorSubject(null);
currentMessage = this.messageSource.asObservable();
getProjectList(messageSource: string[]) {
this.messageSource.next(messageSource);
}
constructor(private http:HttpClient) {
getAllProjectService(teamid,serviceid){
return this.http.get(environment.TEST_AUTOMATION_ENDPOINT+'/api/projects-by-team-and-servicestore?
teamId='+teamid+'&serviceStoreId='+serviceid+'&access_token='+this.decaccesstoken);
}
}
我的 .spec 文件我正在创建一个像这样的间谍对象 -
const mockProjectService= jasmine.createSpyObj('projectService', ['getAllProjectService','currentMessage']);
因为 'currentMessage' 不是服务内部的方法,所以我得到了这个错误。知道如何订阅服务内的变量吗?
将您的服务代码更改为:
messageSource = new BehaviorSubject(null);
currentMessage() {
return this.messageSource.asObservable();
}
getProjectList(messageSource: string[]) {
this.messageSource.next(messageSource);
}
constructor(private http:HttpClient) {
getAllProjectService(teamid,serviceid){
return this.http.get(environment.TEST_AUTOMATION_ENDPOINT+'/api/projects-by-team-
and-servicestore?teamId='+teamid+'&serviceStoreId='+serviceid+'&access_token='+this.decaccesstoken);
}
}
在 karma 单元测试中我得到 this.mobilityService.currentMessage.subscribe is not a function 错误
下面是我试图通过服务从另一个组件获取值的 .ts 文件
constructor(private projectService: ProjectService, private teamService: TeamserviceService) {this.getData()}
getData(){
this.projectService.currentMessage.subscribe(data => {
if(data!=null)
{
this.serviceName=data.service_object.serviceName;
this.serviceId = data.service_object.id})
这是我的服务代码的样子 -
messageSource = new BehaviorSubject(null);
currentMessage = this.messageSource.asObservable();
getProjectList(messageSource: string[]) {
this.messageSource.next(messageSource);
}
constructor(private http:HttpClient) {
getAllProjectService(teamid,serviceid){
return this.http.get(environment.TEST_AUTOMATION_ENDPOINT+'/api/projects-by-team-and-servicestore?
teamId='+teamid+'&serviceStoreId='+serviceid+'&access_token='+this.decaccesstoken);
}
}
我的 .spec 文件我正在创建一个像这样的间谍对象 -
const mockProjectService= jasmine.createSpyObj('projectService', ['getAllProjectService','currentMessage']);
因为 'currentMessage' 不是服务内部的方法,所以我得到了这个错误。知道如何订阅服务内的变量吗?
将您的服务代码更改为:
messageSource = new BehaviorSubject(null);
currentMessage() {
return this.messageSource.asObservable();
}
getProjectList(messageSource: string[]) {
this.messageSource.next(messageSource);
}
constructor(private http:HttpClient) {
getAllProjectService(teamid,serviceid){
return this.http.get(environment.TEST_AUTOMATION_ENDPOINT+'/api/projects-by-team-
and-servicestore?teamId='+teamid+'&serviceStoreId='+serviceid+'&access_token='+this.decaccesstoken);
}
}