[英]undefined-is-not-an-object-evaluating-this-state-datasource.map
[英]unit test undefined is not an object (evaluating 'this.groups.map')
我有那个剧本
groups: Group[] = []
constructor(
) {
this.groups = AuthenticationService.getUserGroups()
let menuList: any = []
this.groups.map((permission: Group) => {
menuList.push(...this.menuGenerator[permission.nomeGrupo.split('-')[1]])
})
}
当我运行npm run test
,只是出现错误TypeError: undefined is not an object (evaluating 'this.groups.map')
。 我想知道什么是最好的解决方法。 我把spyOn
到getUserGroups
,但不起作用。 如何为变量添加一些值? 非常感谢! 我真的需要学习!
放置间谍将取代您的功能。
这意味着它不再返回任何内容。
这意味着您从
groups = [];
然后你监视你的功能
spyOn(AuthenticationService, 'getUserGroups');
然后你得到
groups = undefined;
要解决这个问题,只需在间谍中返回一个值:
spyOn(AuthenticationService, 'getUserGroups').and.returnValue([]);
编辑因为您在构造函数中,所以您无法监视该组件本身,因为它没有被创建。
高兴地为您提供原型继承。 除了监视服务实例,您还可以监视原型:
spyOn(AuthenticationService.prototype, 'getUserGroups').and.returnValue([]);
您应该将此权限放在describe('MyComponent')
,因此这是第一件事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.