Shamoon
Shamoon

Reputation: 43491

Spy on Angular5 component not working properly

My component has the following function:

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

My test is as follows:

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

And I've defined the various services in:

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

Yet, when I run the test, I get an error:

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

What am I doing incorrectly?

Upvotes: 1

Views: 370

Answers (2)

Andrew Eisenberg
Andrew Eisenberg

Reputation: 28747

I don't know why getNoteCreated is not found on noteService. Is it supposed to be there?

Regardless, you can avoid the error by creating the spy in this way:

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

Upvotes: 1

Amit Chigadani
Amit Chigadani

Reputation: 29715

You are trying to return the subscription from getNoteValue instead of an Observable.

Change your spy this way :

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

Upvotes: 0

Related Questions