mercredi 5 juillet 2017

Angular Karma test does not work with angular-in-memory-web-api

angular-in-memory-web-api works fine, but when testing, the http promise does not resolve anything

Here is the code:

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { HttpModule } from '@angular/http';
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService }  from '../in-memory-data.service';

describe('AppComponent',() => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      imports: [
        HttpModule,
        InMemoryWebApiModule.forRoot(InMemoryDataService)
      ]
    });
    TestBed.compileComponents();
  });

  describe('get()', () => {
    it('list', () => {
      const fixture = TestBed.createComponent(AppComponent);
      const app = fixture.debugElement.componentInstance;
      app.service.getItems().then(res => {
        fixture.detectChanges();
        console.log(res);
        expect(app.items.length).toBeGreaterThan(0);
      })
      .catch(err => console.log(err));
    })
  });
})

console.log(res); is never happens, because, function then() is not called

Inside the service http.get(...).toPromise().then(...) also is not called, but only in testing environment

Aucun commentaire:

Enregistrer un commentaire