Angular Karma测试不适用于angular-in-memory-web-api

时间:2017-07-05 14:33:04

标签: angular testing karma-jasmine angular-services

angular-in-memory-web-api工作正常,但在测试时,http承诺无法解决任何问题

以下是代码:

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);永远不会发生,因为函数then()不会被调用

服务this.http.get(...).toPromise().then(...)内部也未被调用,但仅在测试环境中

0 个答案:

没有答案