![](/img/trans.png)
[英]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.