Angular 未捕获类型错误:无法读取未定义抛出-Karma的属性“teams”
我在运行ng测试时遇到了这个问题。它编译得很好,ng build-prod运行得很好。我甚至看不到那些有这个名字的变量,只看到了可观测的,但在变量前面有一个$infront 问题更新 @类型/茉莉花:2.8.9, @类型/jasminewd2:~2.0.3, @类型/节点:~8.9.4, 编码器:~4.2.1, 茉莉花芯:~2.99.1, jasmine spec记者:~4.2.1, 业力:~1.7.1, 卡玛铬发射器:~2.2.0, 因果报应报道伊斯坦布尔记者:~2.0.0, 卡玛·贾斯敏:~1.1.1, karma jasmine html reporter:^0.2.2, 量角器:^5.4.2, ts节点:~5.0.1, tslint:~5.9.1, 类型脚本:~2.7.2 这就是我在因果报应开启时所看到的一切 我的命令行 父测试文件 子测试文件Angular 未捕获类型错误:无法读取未定义抛出-Karma的属性“teams”,angular,unit-testing,jasmine,karma-jasmine,Angular,Unit Testing,Jasmine,Karma Jasmine,我在运行ng测试时遇到了这个问题。它编译得很好,ng build-prod运行得很好。我甚至看不到那些有这个名字的变量,只看到了可观测的,但在变量前面有一个$infront 问题更新 @类型/茉莉花:2.8.9, @类型/jasminewd2:~2.0.3, @类型/节点:~8.9.4, 编码器:~4.2.1, 茉莉花芯:~2.99.1, jasmine spec记者:~4.2.1, 业力:~1.7.1, 卡玛铬发射器:~2.2.0, 因果报应报道伊斯坦布尔记者:~2.0.0, 卡玛·贾斯敏:~
如果您甚至不想测试存储,请尝试以下操作:
import { of } from 'rxjs';
class StoreStub {
select(val: any) {
if (val === 'whatever action you have defined for RacingSelectors.selectMembers') {
return of<Member[]>([
{}, // create dummy member objects
]);
} else if (val === 'whatever action you have defined for RacingSelectors.selectTeams') {
return of<Team[]>([
{ firstName: '' },
{ lastName: '' },
{ jobTitle: '' },
{ team: '' },
{ status: '' }, // create dummy member objects
]);
}
}
dispatch() {}
}
此.store未定义。通常,服务是在测试中模拟的。你的模拟像什么样子?@Gosha_Fighten我真的不知道怎么做。我是Karma的新手,请分享您的Karma测试代码。@Gosha_Fighten doney您正在使用RxJS BehaviorSubject模仿NgRx商店。BehaviorSubject不提供相同的API。它没有像dispatch和select这样的方法。看看它。使用文章中描述的NgRx商店模拟。嗨!谢谢你花时间帮我。问题似乎出在“团队”上。我已将您的代码更改为团队[]的数组,而不是memebers,并且我仍然拥有相同的代码issue@PatricioVargas我将其创建为一个成员数组,因为$members:Observable;具有成员[]类型。我认为未定义错误的分派必须已经解决。我看不到任何与团队相关的代码。该代码似乎给出了无法读取的未定义团队。这是RacingActions类中的内容吗?抱歉,我删除了“`$teams:Observable;”这行在我的问题中,这是偶然的。我又加了一次。我注意到调度问题已经解决了。唯一持续出现两次的是UncaughtTypeError:无法读取未定义抛出的属性“teams”,因为我已更新了question@PatricioVargas:我仍然看不到。请使用您提供的代码。你可以添加整个错误的屏幕截图。它显示了发生此错误的行号。这不是从您为sureYep提供的代码中发生的!!我看不到。该组件中的团队甚至不在html页面中。我想这是因为,团队是我所在州的一部分。我的状态如下{成员:成员[],团队:团队[],selectedMember:Member}
describe('ModalComponent', () => {
let component: ModalComponent;
let fixture: ComponentFixture<ModalComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ModalComponent],
imports: [FormsModule, ReactiveFormsModule, StoreModule.forRoot({})],
providers: [Store]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ModalComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { of } from 'rxjs';
class StoreStub {
select(val: any) {
if (val === 'whatever action you have defined for RacingSelectors.selectMembers') {
return of<Member[]>([
{}, // create dummy member objects
]);
} else if (val === 'whatever action you have defined for RacingSelectors.selectTeams') {
return of<Team[]>([
{ firstName: '' },
{ lastName: '' },
{ jobTitle: '' },
{ team: '' },
{ status: '' }, // create dummy member objects
]);
}
}
dispatch() {}
}
TestBed.configureTestingModule({
imports: [
ReactiveFormsModule,
HttpClientModule,
RouterTestingModule,
StoreModule.forRoot({})
],
declarations: [MembersComponent, ModalComponent],
providers: [{ provide: Store, useClass: StoreStub}]
}).compileComponents();
}));
it('should create', () => {
component.ngOnInit();
expect(component).toBeTruthy();
});