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/Jasmine单元测试中的spyService和throwError()问题;_Angular_Unit Testing_Jasmine - Fatal编程技术网

Angular/Jasmine单元测试中的spyService和throwError()问题;

Angular/Jasmine单元测试中的spyService和throwError()问题;,angular,unit-testing,jasmine,Angular,Unit Testing,Jasmine,我无法模拟在组件的单元测试文件中返回错误的服务。我的应用程序是用Angular 6和TypeScript编写的,我的单元测试是用Jasmine编写的 在我的组件中,我调用了ngOnInit中的一个方法,在这个方法中,我调用了导入服务上的一个方法,如果失败,我的组件中名为isLoading的布尔属性应该设置为false。下面是我的组件的一些最小化代码,让我了解我希望测试的代码 导出类CylindersListComponent实现OnInit、OnDestroy{ 公共isLoading:布尔值=

我无法模拟在组件的单元测试文件中返回错误的服务。我的应用程序是用Angular 6和TypeScript编写的,我的单元测试是用Jasmine编写的

在我的组件中,我调用了
ngOnInit
中的一个方法,在这个方法中,我调用了导入服务上的一个方法,如果失败,我的组件中名为
isLoading
的布尔属性应该设置为false。下面是我的组件的一些最小化代码,让我了解我希望测试的代码

导出类CylindersListComponent实现OnInit、OnDestroy{ 公共isLoading:布尔值=true; public ngOnInit():void{ //做一些事情…我已经删除了一些代码在这里 这个.loadCylinderList(); } 公共loadCylinderList(){ this.cylindersService.getCylinderList().subscribe((res:any)=>{ 此参数=res; //我已经删除了很多代码 this.isLoading=false; },(错误:任意)=>{ this.isLoading=false; 抛出新错误(Error); }); } } 我希望模拟调用的
loadCylinderList
方法以及cylindersService.getCylinderList返回错误。因此,一旦完成此操作,我希望断言(或确保)
isLoading
属性为false。现在我已经这样设置了我的单元测试,但是这似乎不起作用(或者更可能是我错误地实现了测试)。我再次最小化了代码或将
放在返回数据的位置

description('CylindersListComponent',()=>{
let夹具:组件夹具;
例如:CylindersListComponent;
const spyCylinderService=jasmine.createSpyObj(
“CylindersService”、“getCylinderList”、“getListPaged”];
spyCylinderService.getListPaged.and.returnValue(observedData);//observedData在文件中设置得越早
beforeach(异步(()=>{
//注入间谍。我们使用覆盖组件,因为服务是注入的
//组件中不在模块中。(更深层次)
测试床.重写组件(CylindersListComponent{
设置:{
供应商:[
{提供:CylindersService,useValue:spyCylinderService}
],
模板:“”
}
})
//为测试准备气缸部件
//允许覆盖默认提供程序、指令、管道
.配置测试模块({
进口:[……],
声明:[
气缸滑动部件
],
模式:[自定义元素\u模式]
})
.compileComponents()。然后(()=>{
//安排
fixture=TestBed.createComponent(CylindersListComponent);
实例=fixture.componentInstance;
spyCylinderService.getCylinderList.and.returnValue({…});
});
}));
它('加载数据时出错后应为false',()=>{
//安排
spyCylinderService.getCylinderList.and.throwError('error');
instance.isLoading=true;
spyOn(实例'loadCylinderList'如有)。和.callThrough();
//表演
fixture.detectChanges();
expect(instance.isLoading).toBe(false);
});
这听起来像是一个非常开放的问题,我很抱歉,但是我的test/spyOn方法做错了什么。我确信我得到了一个失败的测试/错误,因为我提出/模拟错误的实现是不正确的:
spyCylinderService.getCylinderList.and.throwError('error');

如果有人能看到我做错了什么,我将不胜感激。如果有帮助,测试控制台的错误如下:

HeadlessChrome 0.0.0 (Mac OS X 10.12.6) CylindersListComponent isLoading should be false after error while loading data FAILED
    Error: error
    error properties: Object({ ngDebugContext: DebugContext_({ view: Object({ def: Object({ factory: Function, nodeFlags: 33800449, rootNodeFlags: 33554433, nodeMatchedQueries: 0, flags: 0, nodes: [ Object({ nodeIndex: 0, parent: null, renderParent: null, bindingIndex: 0, outputIndex: 0, checkIndex: 0, flags: 33554433, childFlags: 246016, directChildFlags: 246016, childMatchedQueries: 0, matchedQueries: Object({  }), matchedQueryIds: 0, references: Object({  }), ngContentIndex: null, childCount: 4, bindings: [  ], bindingFlags: 0, outputs: [  ], element: Object({ ns: '', name: 'cylinders', attrs: [  ], template: null, componentProvider: Object({ nodeIndex: 4, parent: <circular reference: Object>, renderParent: <circular reference: Object>, bindingIndex: 0, outputIndex: 0, checkIndex: 4, flags: 245760, childFlags: 0, directChildFlags: 0, childMatchedQueries: 0, matchedQueries: Object, matchedQueryIds: 0, references: Object, ngContentIndex: -1, childCount: 0, bindings: Array, bindingFlags: 0, outputs: Array,  ...
        at <Jasmine>
加载数据失败时出错后,HeadlessChrome 0.0.0(Mac OS X 10.12.6)CylindersListComponent isLoading应为false 错误:错误 错误属性:对象({ngDebugContext:DebugContext}{view:Object({factory:Function,nodeFlags:33800449,rootNodeFlags:33554433,nodeMatchedQueries:0,标志:0,节点:[Object({nodeIndex:0,parent:null,renderParent:null,bindingIndex:0,outputIndex:0,checkIndex:0,flags:33554433,childFlags:246016,directChildFlags:246016,childMatchedQueries:0,matchedQueries:Object({}),MatchedQueryId:0,引用:Object({}),ngContentIndex:null,childCount:4,绑定:[],bindingFlags:0,输出:[],元素:对象({ns:'',名称:'圆柱体',属性:[],模板:null,组件提供程序:对象({nodeIndex:4,parent:,renderParent:,bindingIndex:0,outputIndex:0,checkIndex:4,标志:245760,childFlags:0,directChildFlags:0,childMatchedQueries:0,MatchedQueryId:0,MatchedQueryId:0,引用:对象,ngContentIndex:-1,childCount:0,绑定:数组,bindingFlags:0,输出:数组。。。 在
更新:我确信我提出错误的方式是错误的,好像我应该在捕获错误的组件中放置一个
控制台。日志
,没有任何内容写入控制台。

原因可能是isLoading是异步更新的。因此,您应该等到所有更改检测和更新结束

it('加载数据时出错后应为false',异步(()=>{
//安排
spyCylinderService.getCylinderList.and.returnValue(Observable.throw('error'));
instance.isLoading=true;
spyOn(实例'loadCylinderList'如有)。和.callThrough();
//表演
fixture.detectChanges();
fixture.whenStable()
.然后(()=>{
expect(instance.isLoading).toBe(false);
});    
}));

原因可能是isLoading是异步更新的。因此,您应该等到所有更改检测和更新结束

it('加载数据时出错后应为false',异步(()=>{
//安排
spyCylinderService.getCylinderList.and.returnValue(Observable.throw('error'));
instance.isLoading=true;
spyOn(实例'loadCylinderList'如有)。和.callThrough();
//表演
fixture.detectChanges();
fixture.whenStable()
.然后(()=>{
期望(实例)