admin管理员组文章数量:1415139
I have a service which I would like to test:
@Injectable()
export class AuthenticationService {
initialAuthStatus = this.authenticationStatus.first();
constructor(private adalService: AdalService,
private settingsProvider: SettingsProvider) { }
public get authenticationStatus(): any {
return this.adalService.userInfo.authenticationStatus;
}
}
And a test for the service:
describe('AuthenticationService', () => {
let mockAdalService: any;
let adalService: any;
let service: any;
let mockSettingsProvider: any;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
AuthenticationService,
AdalService,
{ provide: SettingsProvider, useClass: MockSettingsProvider },
{ provide: AdalService, useClass: MockAdalService }
]
});
mockAdalService = new MockAdalService();
adalService = new AdalService();
mockSettingsProvider = new MockSettingsProvider();
});
it('should be created', inject([AuthenticationService], (service: AuthenticationService) => {
expect(service).toBeTruthy();
}));
});
However, the test fails with the following error message:
AuthenticationService should be created
TypeError: Cannot read property 'authenticationStatus' of undefined
It has something to do with the getter of authentication status, but I can't figure out exactly why it fails.
Any help is greatly appreciated :)
I have a service which I would like to test:
@Injectable()
export class AuthenticationService {
initialAuthStatus = this.authenticationStatus.first();
constructor(private adalService: AdalService,
private settingsProvider: SettingsProvider) { }
public get authenticationStatus(): any {
return this.adalService.userInfo.authenticationStatus;
}
}
And a test for the service:
describe('AuthenticationService', () => {
let mockAdalService: any;
let adalService: any;
let service: any;
let mockSettingsProvider: any;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
AuthenticationService,
AdalService,
{ provide: SettingsProvider, useClass: MockSettingsProvider },
{ provide: AdalService, useClass: MockAdalService }
]
});
mockAdalService = new MockAdalService();
adalService = new AdalService();
mockSettingsProvider = new MockSettingsProvider();
});
it('should be created', inject([AuthenticationService], (service: AuthenticationService) => {
expect(service).toBeTruthy();
}));
});
However, the test fails with the following error message:
AuthenticationService should be created
TypeError: Cannot read property 'authenticationStatus' of undefined
It has something to do with the getter of authentication status, but I can't figure out exactly why it fails.
Any help is greatly appreciated :)
Share Improve this question edited Mar 29, 2020 at 0:48 Vega 28.8k28 gold badges120 silver badges145 bronze badges asked Feb 16, 2018 at 11:33 dave0688dave0688 5,7907 gold badges39 silver badges69 bronze badges 4- Error es from this line : initialAuthStatus = this.authenticationStatus.first(); it's hasn't been initialized at that point – Vega Commented Feb 16, 2018 at 11:45
- How can I solve it? – dave0688 Commented Feb 16, 2018 at 11:46
- put it in the constructor or ngOnInit and leave just initialAuthStatus at the top – Vega Commented Feb 16, 2018 at 11:46
- Wow, ok I was really blind. I've put it in ngOnInit now (constructor didn't work), and it works now. Thanks a log :) Please write a post so that I can mark it as the answer. – dave0688 Commented Feb 16, 2018 at 11:51
1 Answer
Reset to default 1At the top of the class, in this line, where you declare the class property initialAuthStatus
:
initialAuthStatus = this.authenticationStatus.first();
this.authenticationStatus
hasn't been initialized yet, gives you the error message.
To make it work, put that line in ngOnInit() method and keep pure declaration part at the top of the class.
initialAuthStatus;
...
ngOnInit(){
this.initialAuthStatus = this.authenticationStatus;
}
...
...
本文标签: javascriptKarma Cannot read property * of undefinedStack Overflow
版权声明:本文标题:javascript - Karma: Cannot read property * of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745206950a2647684.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论