我想在触发更改检测之前调用created spy方法.这是我的模拟课.

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();
  });

我想这么做是因为这个问题.但根据我的模拟服务https://stackoverflow.com/questions/69989284/...,我不知道如何改变解决方案

推荐答案

您需要在此处使用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();
});

Angular相关问答推荐

如何避免使用CSS动画制作的幻灯片中闪烁?

在Angular中将路由解析器提供程序和组件提供程序相结合

PrimeNG面包屑组件生成奇怪的&

命令不起作用的初始菜单项

有没有其他代码可以用函数的方式写成Angular ?

NG-Zorro Datepicker 手动输入掩码不起作用

使用forkJoin和mergeMap的正确方式是什么?

重新打开模态时 ng-select 中的重复值 - Angular

以 Angular 形式添加动态控件失败

RxJs 中的空闲可观察对象在幕后做了什么?

iss 声明无效 Keycloak

Angular keypress 存储空间而不是第一个字母

个人修改 Angular CLI - 创建新应用

Angular2:更新数组时*ngFor不更新

ngx-bootstrap modal:如何从模态中获取返回值?

如何在不刷新整页的情况下更新组件

如何使用Angular4通过元素ID设置焦点

无法从模块it was neither declared nor imported导出服务

Angular 2 中的 OnChanges 和 DoCheck 有什么区别?

Angular单元测试输入值