【问题标题】:Spy on Angular5 component not working properly窥探 Angular5 组件无法正常工作
【发布时间】:2018-07-14 15:49:55
【问题描述】:

我的组件有以下功能:

  updateTransactions() {
    let notes = this.createNotes()

    let delTransactions = this.createDelTransactions()
    this.noteService.createNote(notes[0])
    this.noteService.getNoteCreated().subscribe((res) => {
      notes.shift();
      if(res && notes.length > 0) {
        this.noteService.createNote(notes[0])
      } else {
        this.deliverableTransactionService.updateDeliverableTransaction(delTransactions[0].id, delTransactions[0])
      }
    })

    this.deliverableTransactionService.getUpdateDeliverableTransactionStatus().subscribe((res) => {
      delTransactions.shift();
      if(res && delTransactions.length > 0) {
        this.deliverableTransactionService.updateDeliverableTransaction(delTransactions[0].id, delTransactions[0])
      } else {
        this.closeModal();
      }
    })
    console.log('current note', this.note)
  }

我的测试如下:

  it('should initialize properly when updating', () => {
    let testTransactions = [{id: 1}]
    let dueDate = '1/2/3'
    let testNote = 'Test'
    component.deliverableTransactions = testTransactions
    component.dueDate = dueDate
    component.note = testNote

    spyOn(component, 'createNotes').and.callThrough();
    spyOn(component, 'createDelTransactions').and.callThrough();
    spyOn(noteService, 'createNote').and.returnValue(true);
    spyOn(noteService, 'getNoteCreated').and.returnValue({subscribe: () => true});
    spyOn(deliverableTransactionService, 'getUpdateDeliverableTransactionStatus').and.returnValue({subscribe: () => true});

    component.updateTransactions();

    expect(component.createNotes).toHaveBeenCalled();
    expect(component.createDelTransactions).toHaveBeenCalled();
    expect(noteService.createNote).toHaveBeenCalled();
    expect(noteService.getNoteCreated).toHaveBeenCalled();
    expect(deliverableTransactionService.getUpdateDeliverableTransactionStatus).toHaveBeenCalled();
  })

我已经定义了各种服务:

  beforeEach(async(() => {
    fixture = TestBed.createComponent(ChangeDueDateComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    deliverableTransactionService = TestBed.get(DeliverableTransactionService);
    noteService = TestBed.get(NoteService);
    activeModal = TestBed.get(NgbActiveModal);
  }));

然而,当我运行测试时,我得到一个错误:

PhantomJS 2.1.1 (Windows 7 0.0.0) ChangeDueDateComponent should initialize properly when updating FAILED
        Error: <spyOn> : getNoteCreated() method does not exist

我做错了什么?

【问题讨论】:

  • 您能否将其缩减为minimal reproducible example,包括您正在监视的服务?另请注意,您可以返回 of(true) 而不是使用 subscribe 属性构建对象。

标签: javascript angular unit-testing jasmine spy


【解决方案1】:

我不知道为什么在noteService 上找不到getNoteCreated。它应该在那里吗?

无论如何,您可以通过这种方式创建间谍来避免错误:

noteService.getNoteCreated = jasmine.createSpy().and
  .returnValue({subscribe: () => true});

【讨论】:

    【解决方案2】:

    您正尝试从getNoteValue 而不是Observable 返回订阅。

    这样改变你的间谍:

    spyOn(noteService, 'getNoteCreated').and.returnValue(Observable.of(true));
    

    【讨论】:

    • 有帮助,但仍然出现无法找到getNoteCreated的相同错误
    • 这不是答案;错误在于设置间谍,它甚至没有得到返回值。
    • @Shamoon 你确定,你的应用可以正常运行,没有任何错误吗?
    • 是的 - 应用程序可以正常运行而不会出错
    猜你喜欢
    • 2020-02-24
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2021-08-15
    • 2020-04-11
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多