Angular 无法读取 属性 订阅未定义错误
Angular cannot read property subscribe of undefined error
我遇到了这个奇怪的无法读取未定义错误的订阅,奇怪的是,当它独立 运行 时不会出现,但几乎总是在 运行 不使用 focused 的情况下出现测试
我不知道为什么我会收到这个 error.I 尝试了各种网站上提到的许多事情,例如添加一个 afterEach 循环。
有人可以指出为什么我的代码出错
你的间谍在你到达测试用例之前被调用:
beforeEach(() => {
fixture = TestBed.createComponent(ViewAccessGroupComponent); // HERE, the constructor is called
component = fixture.componentInstance;
fixture.detectChanges();
});
您可以将订阅移动到 ngOnInit
,然后订阅将在调用 fixture.detectChanges()
时完成,因此您可以将该调用移动到下面设置间谍结果:
it("#getAccessGroup should show error message if API fails ", () => {
accessGroupSpy.getAccessGroup.and.returnValue(throwError({error:{message:"error"}}))
fixture.detectChanges(); // HERE instead of in beforeEach
我遇到了这个奇怪的无法读取未定义错误的订阅,奇怪的是,当它独立 运行 时不会出现,但几乎总是在 运行 不使用 focused 的情况下出现测试
我不知道为什么我会收到这个 error.I 尝试了各种网站上提到的许多事情,例如添加一个 afterEach 循环。
有人可以指出为什么我的代码出错
你的间谍在你到达测试用例之前被调用:
beforeEach(() => {
fixture = TestBed.createComponent(ViewAccessGroupComponent); // HERE, the constructor is called
component = fixture.componentInstance;
fixture.detectChanges();
});
您可以将订阅移动到 ngOnInit
,然后订阅将在调用 fixture.detectChanges()
时完成,因此您可以将该调用移动到下面设置间谍结果:
it("#getAccessGroup should show error message if API fails ", () => {
accessGroupSpy.getAccessGroup.and.returnValue(throwError({error:{message:"error"}}))
fixture.detectChanges(); // HERE instead of in beforeEach