0

我正在使用 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 提供者! UniversalModule如注释中所示,在 app.module 中应该 http已经导入模块。

我正在使用最新的 Angular 通用。

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

4

1 回答 1

0

这篇文章给了我一个如何解决它的想法: http: //chariotsolutions.com/blog/post/testing-angular-2-0-x-services-http-jasmine-karma/

于 2017-01-25T16:24:17.810 回答