1
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.

Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
mksmanjit
  • 244
  • 2
  • 12

2 Answers2

1

Just remove from. It returns an Observable instead of Promise

spyOn(service, 'getTodosPromise').and.returnValue(Promise.resolve([1, 2, 3]));
Amit Chigadani
  • 28,482
  • 13
  • 80
  • 98
1

You should not use from. it returns observable :

spyOn(service, 'getTodosPromise').and.returnValue(Promise.resolve([1, 2, 3]));
Akj
  • 7,038
  • 3
  • 28
  • 40