Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 this.mobilityService.currentMessage.subscribe不是角度单元测试中的函数_Angular_Unit Testing_Jasmine_Observable_Karma Runner - Fatal编程技术网

Angular this.mobilityService.currentMessage.subscribe不是角度单元测试中的函数

Angular this.mobilityService.currentMessage.subscribe不是角度单元测试中的函数,angular,unit-testing,jasmine,observable,karma-runner,Angular,Unit Testing,Jasmine,Observable,Karma Runner,在karma单元测试中,我发现.mobilityService.currentMessage.subscribe不是一个函数错误 下面是我试图通过服务从另一个组件获取值的.ts文件 constructor(private projectService: ProjectService, private teamService: TeamserviceService) {this.getData()} getData(){ this.projectService.currentMessage.

在karma单元测试中,我发现.mobilityService.currentMessage.subscribe不是一个函数错误

下面是我试图通过服务从另一个组件获取值的.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);
 }
 }

您好,谢谢您的回复,但我仍然收到TypeError:无法读取getData()方法中未定义错误的属性'subscribe',this.projectService.currentMessage()是方法OK它工作正常,因为我没有在.spec文件中写入returnvalue,所以它抛出了错误。谢谢你的回答。