被调用的spyOn返回未使用angular5单元测试调用的spyOn

被调用的spyOn返回未使用angular5单元测试调用的spyOn,angular,unit-testing,spy,Angular,Unit Testing,Spy,我的组件有: export class JsonformComponent implements OnInit { @Input() dataplanDetails: any; public layout: any = []; public schema: any = {}; ngOnInit() { this.dataplanDetails.subscribe(res => { return this.parseSchema(res.details.

我的组件有:

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();
  })
fdescribe('JsonformComponent',()=>{
let组件:JsonformComponent;
let夹具:组件夹具;
const mockObservable=新主题();
beforeach(异步(()=>{
TestBed.configureTestingModule({
模式:[自定义元素模式,无错误模式],
供应商:[
{provide:Store,useClass:StoreStub}
],
进口:[],
声明:[JsonformComponent]
}).compileComponents();
}));
beforeach(异步(()=>{
fixture=TestBed.createComponent(JsonformComponent);
组件=fixture.componentInstance;
组件['dataplanDetails']=mockObservable
fixture.detectChanges();
}));
它('应该创建',()=>{
expect(component.toBeTruthy();
});
它('应该触发parseSchema事件',()=>{
mockObservable.next({“details”:{“submissionFileSchema”:`{“properties”:true,“layout”:[true]}`})
spyOn(组件'parseSchema')。和.returnValue(true);
expect(component.parseSchema).tohaveBeenCall();
})

console.log
会触发,因此它肯定在
parseSchema
函数中。但是测试失败,因为
预期已调用spy parseSchema。
在可观察对象触发该代码后,您正在设置spy。请将您的spy移动到将数据推送到主题上的位置上方

it('应该触发parseSchema事件',()=>{
spyOn(组件'parseSchema')。和.returnValue(true);
mockObservable.next({“details”:{“submissionFileSchema”:`{“properties”:true,“layout”:[true]}`})
expect(component.parseSchema).tohaveBeenCall();
})