mksmanjit
mksmanjit

Reputation: 244

Failed: this.service.getTodosPromise(...).then is not a function

This is the code where it is breaking:

**ngOnInit() {
    this.service.getTodosPromise().then(t => {
      this.todos = t; });
  }**

and this is the getTodosPromise() method in the Service:

**getTodosPromise() {
    return this.http.get('...').pipe(map(r => r.json())).toPromise();
  }**

And in the *.spect.ts file i have one test for which it is breaking:

  **it('should load todos from the server', async(() => {
     const service = TestBed.get(TodoService);
    // This is used if provider is given at component level.
     fixture.debugElement.injector.get(TodoService);
     spyOn(service, 'getTodosPromise').and.returnValue(from(Promise.resolve([1, 2, 3])));

     fixture.detectChanges();

     fixture.whenStable().then(() => {
       expect(component.todos.length).toBe(3);
       console.log('EXPECT WAS CALLED1');
     });
  }));**

I am using the angular 6 and didn't see any compilation error but my test case is failing because of above error.

Upvotes: 1

Views: 118

Answers (2)

Akj
Akj

Reputation: 7231

You should not use from. it returns observable :

spyOn(service, 'getTodosPromise').and.returnValue(Promise.resolve([1, 2, 3]));

Upvotes: 1

Amit Chigadani
Amit Chigadani

Reputation: 29745

Just remove from. It returns an Observable instead of Promise

spyOn(service, 'getTodosPromise').and.returnValue(Promise.resolve([1, 2, 3]));

Upvotes: 1

Related Questions