Angular 2 Universal,单元测试失败并显示错误,没有Http的提供者

时间:2017-01-25 12:04:51

标签: http angular jasmine angular-universal

我使用Angular 2 Universal:

我有服务:

import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Page } from './page';

@Injectable()
export class MyService {

  constructor(private http: Http) { }
  getPage(id: number): Observable<Page> {
    return null;
  }


}

单元测试:

import { TestBed, async, inject } from '@angular/core/testing';
import { PageService } from './workflow.service';

describe('Service: Workflow', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [WorkflowService]
    });
  });

  it('should ...', inject([PageService], (service: PageService) => {
    expect(service).toBeTruthy();
  }));

});

我的应用模块:

@NgModule({
  bootstrap: [AppComponent],
  declarations: [
    AppComponent,
    HomeComponent,
    WorkflowComponent
  ],
  imports: [
    HttpModule,
    UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
    RouterModule.forRoot([
      { path: '', redirectTo: 'home', pathMatch: 'full' },
      { path: 'home', component: HomeComponent },
      { path: 'workflow/:id', component: WorkflowComponent }
    ])
  ]
})
export class AppModule {
}

当我运行单元测试时,我得到:错误:没有Http的提供者! app.module中的UniversalModule应该已经按照评论中的说明导入http模块。

我正在使用最新的Angular universal。

我应该在测试中添加http吗?

1 个答案:

答案 0 :(得分:0)