Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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:x(…)。订阅不是函数_Angular_Unit Testing_Components - Fatal编程技术网

Angular TypeError:x(…)。订阅不是函数

Angular TypeError:x(…)。订阅不是函数,angular,unit-testing,components,Angular,Unit Testing,Components,我试图对调用ngOnInit中服务方法的组件进行单元测试。当我尝试运行测试时,我得到错误: TypeError:this.adminService.getTestString(…).subscribe不是函数 以下是供参考的文件 app.component.ts @组件({ 选择器:'应用程序根', templateUrl:“./app.component.html”, 样式URL:['./app.component.css'] }) 导出类AppComponent{ 标题='ISAppAdmi

我试图对调用ngOnInit中服务方法的组件进行单元测试。当我尝试运行测试时,我得到错误:

TypeError:this.adminService.getTestString(…).subscribe不是函数

以下是供参考的文件

app.component.ts
@组件({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
标题='ISAppAdmin';
问候:绳子;
错误消息:字符串;
构造函数(专用adminService:adminService,专用路由器:路由器){}
恩戈尼尼特(){
此.adminService.getTestString().subscribe(
data=>this.greet=data,
error=>this.errorMessage=错误
)
此.adminService.putTestString().subscribe(
res=>console.log(res),
error=>this.errorMessage=错误
)
console.log(“in-app”);
}
onClick(){
控制台日志(“点击按钮”);
this.router.navigate(['admin']);
console.log(“仍在同一页上”);
}

}
您的模拟服务不会返回可观察的:

import { of } from 'rxjs';

class MockAdminService {
  res = "Hello World";

  getTestString() {
    return of(this.res);
  }
}

您的模拟服务不会返回可观察的:

import { of } from 'rxjs';

class MockAdminService {
  res = "Hello World";

  getTestString() {
    return of(this.res);
  }
}

AdminService
通过执行
返回this.http.get(…)
来返回一个可观察的对象。但是
MockAdminService
不返回可观察的。在
MockAdminService
上创建一个间谍以返回可观察的
AdminService
通过执行
返回此.http.get(…)
返回可观察的。但是
MockAdminService
不返回可观察对象。在
MockAdminService
上创建间谍以返回可观察对象返回可观察对象是解决方案。谢谢你的帮助。返回observable是解决方案。谢谢你的帮助。