简体   繁体   中英

Angular 2 Universal, unit test fails with an error, No provider for Http

I'm usung Angular 2 Universal:

I have a service:

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;
  }


}

Unit test:

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();
  }));

});

My app module:

@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 {
}

When I run unit test I get: Error: No provider for Http! UniversalModule in app.module should import http module already as indicated in the comments.

I'm using the latest Angular universal.

Should I add http in the test?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM