Angular 2通用,单元测试失败,出现错误,没有Http提供程序
我使用角度2通用: 我有一项服务:Angular 2通用,单元测试失败,出现错误,没有Http提供程序,http,angular,jasmine,angular-universal,Http,Angular,Jasmine,Angular Universal,我使用角度2通用: 我有一项服务: 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): Obser
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;
}
}
我的应用程序模块:
@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
模块
我用的是最新的角度通用
我应该在测试中添加
http
吗?这篇文章给了我一个解决方法:
你试过这样的东西吗<代码>提供程序:[{提供:Http,useFactory:(后端:ConnectionBackend,defaultOptions:BaseRequestOptions)=>{返回新的Http(后端,defaultOptions);},deps:[MockBackend,BaseRequestOptions]},]@styopdev感谢您的评论-它给出了一个关于谷歌的想法。
@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 {
}