在更改检测之前调用创建的间谍
Call created spy before change detection
我想在触发更改检测之前调用创建的间谍方法。这是我的嘲笑 class.
const stubStudentService: Pick<StudentsCrudService, keyof StudentsCrudService> =
{
getAllStudents: jasmine
.createSpy('getAllStudents')
.and.returnValue(of(studentPayload)),
...other methods
};
如何在 fixture.detectChanges()
之前调用 getAllStudents
beforeEach(() => {
fixture = TestBed.createComponent(StudentsListComponent);
component = fixture.componentInstance;
<-- want to call that spy here --->
fixture.detectChanges();
});
因为这个问题,我想这样做。但不知道如何根据我的模拟服务更改解决方案
你需要在这里使用TestBed.inject
:
beforeEach(() => {
TestBed.configureTestingModule({
// ...
providers: [
{
provide: StudentsCrudService,
useValue: stubStudentService,
},
],
});
});
beforeEach(() => {
fixture = TestBed.createComponent(StudentsListComponent);
component = fixture.componentInstance;
const studentsCrudService = TestBed.inject(StudentsCrudService);
// spyOn(studentsCrudService, 'getAllStudents')...;
fixture.detectChanges();
});
我想在触发更改检测之前调用创建的间谍方法。这是我的嘲笑 class.
const stubStudentService: Pick<StudentsCrudService, keyof StudentsCrudService> =
{
getAllStudents: jasmine
.createSpy('getAllStudents')
.and.returnValue(of(studentPayload)),
...other methods
};
如何在 fixture.detectChanges()
getAllStudents
beforeEach(() => {
fixture = TestBed.createComponent(StudentsListComponent);
component = fixture.componentInstance;
<-- want to call that spy here --->
fixture.detectChanges();
});
因为这个问题,我想这样做。但不知道如何根据我的模拟服务更改解决方案
你需要在这里使用TestBed.inject
:
beforeEach(() => {
TestBed.configureTestingModule({
// ...
providers: [
{
provide: StudentsCrudService,
useValue: stubStudentService,
},
],
});
});
beforeEach(() => {
fixture = TestBed.createComponent(StudentsListComponent);
component = fixture.componentInstance;
const studentsCrudService = TestBed.inject(StudentsCrudService);
// spyOn(studentsCrudService, 'getAllStudents')...;
fixture.detectChanges();
});