无法读取单元测试 NgOnInIt 中未定义的属性(读取“订阅”)
我尝试了很多SO问题的答案,但没有任何效果。所以在mt测试中我做了这样的事情。
const stubStudentService = jasmine.createSpyObj('StudentsCrudService',['getAllStudents','updateStudent']);
const stubWebSocketService = jasmine.createSpyObj('WebsocketService',['getNotified']);
describe('StudentsListComponent', () => {
let component: StudentsListComponent;
let fixture: ComponentFixture<StudentsListComponent>;;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [StudentsListComponent],
imports: [],
providers: [
{ provide: StudentsCrudService, useValue: stubStudentService },
{ provide: WebsocketService, useValue: stubWebSocketService },
NotificationService,
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(StudentsListComponent);
stubStudentService.getAllStudents.and.returnValue(of(studentPayload));
stubWebSocketService.getNotified.and.returnValue(of('success'));
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
在我的组件中,它看起来像这样
constructor(
private studentService: StudentsCrudService,
private notificationService: NotificationService,
private webSocketNotifier: WebsocketService
) {}
ngOnInit(): void {
this.studentSubscription();
this.webSocketSubscription();
}
studentSubscription() {
this.studentQueryRef = this.studentService
.getAllStudents()
this.initSubscription.add(
this.studentQueryRef
.valueChanges.subscribe((response) => {
this.paginatedStudents = {...response.data.paginateStudents}
})
);
}
当我运行测试时,我得到无法读取StudentsListComponent.studentSubscription中未定义的属性(读取“订阅”)
。我尝试了很多事情但没有任何效果。
I tried a lot of SO Question's answers but nothing worked for this.so in mt test im doing something like this.
const stubStudentService = jasmine.createSpyObj('StudentsCrudService',['getAllStudents','updateStudent']);
const stubWebSocketService = jasmine.createSpyObj('WebsocketService',['getNotified']);
describe('StudentsListComponent', () => {
let component: StudentsListComponent;
let fixture: ComponentFixture<StudentsListComponent>;;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [StudentsListComponent],
imports: [],
providers: [
{ provide: StudentsCrudService, useValue: stubStudentService },
{ provide: WebsocketService, useValue: stubWebSocketService },
NotificationService,
],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(StudentsListComponent);
stubStudentService.getAllStudents.and.returnValue(of(studentPayload));
stubWebSocketService.getNotified.and.returnValue(of('success'));
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
In my component, it look like this
constructor(
private studentService: StudentsCrudService,
private notificationService: NotificationService,
private webSocketNotifier: WebsocketService
) {}
ngOnInit(): void {
this.studentSubscription();
this.webSocketSubscription();
}
studentSubscription() {
this.studentQueryRef = this.studentService
.getAllStudents()
this.initSubscription.add(
this.studentQueryRef
.valueChanges.subscribe((response) => {
this.paginatedStudents = {...response.data.paginateStudents}
})
);
}
When I run the test im getting Cannot read properties of undefined (reading 'subscribe') at StudentsListComponent.studentSubscription
. I tried so many things but nothing worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个
fixture.detectChanges
是在调用ngOnInit
时发生的。您已正确模拟
studentService
,但没有错误地模拟getAllStudents
的返回。试试这个(下面的评论是!!):
上面的内容有望解决您的问题。
The first
fixture.detectChanges
is whenngOnInit
is called.You have mocked
studentService
correctly but you haven't mocked the return ofgetAllStudents
incorrectly.Try this (follow comments with !!):
The above should hopefully fix your problem.