正在调用的 spyOn returns 未使用 angular5 单元测试调用

spyOn that is being called returns not being called with angular5 unit test

我的组件有:

export class JsonformComponent implements OnInit {
  @Input() dataplanDetails: any;
  public layout: any = [];
  public schema: any = {};

  ngOnInit() {
    this.dataplanDetails.subscribe(res => {
      return this.parseSchema(res.details.submissionFileSchema)
    })
  }

  parseSchema(submissionFileSchema) {
    console.log('in parseSchema')
    const fileSchema = JSON.parse(submissionFileSchema)

我的测试是:

fdescribe('JsonformComponent', () => {
  let component: JsonformComponent;
  let fixture: ComponentFixture<JsonformComponent>;
  const mockObservable = new Subject();

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
      providers: [
        {provide: Store, useClass: StoreStub}
      ],
      imports: [],
      declarations: [JsonformComponent]
    }).compileComponents();
  }));

  beforeEach(async(() => {
    fixture = TestBed.createComponent(JsonformComponent);
    component = fixture.componentInstance;
    component['dataplanDetails'] = mockObservable
    fixture.detectChanges();
  }));

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  it('should trigger a parseSchema event', () => {
    mockObservable.next({"details": { "submissionFileSchema": `{"properties": true, "layout": [true]}`}})
    spyOn(component, 'parseSchema').and.returnValue(true);
    expect(component.parseSchema).toHaveBeenCalled();
  })

console.log 触发器,所以它肯定在 parseSchema 函数中。但是测试失败 Expected spy parseSchema to have been called.

在可观察对象触发该代码后,您正在设置间谍。将您的间谍移动到您将数据推送到您的主题的位置上方。

it('should trigger a parseSchema event', () => {
    spyOn(component, 'parseSchema').and.returnValue(true);
    mockObservable.next({"details": { "submissionFileSchema": `{"properties": true, "layout": [true]}`}})
    expect(component.parseSchema).toHaveBeenCalled();
})