gpt4 book ai didi

javascript - 监视 Angular5 组件无法正常工作

转载 作者:行者123 更新时间:2023-11-30 20:22:06 27 4
gpt4 key购买 nike

我的组件具有以下功能:

  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

我做错了什么?

最佳答案

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

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

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

关于javascript - 监视 Angular5 组件无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341000/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com