嗨,我试图进入测试我的组件在Angular2,但遇到了一个问题,当我试图嘲笑我的服务。我目前正在关注测试中的Angular2文档。Angular2测试组件
我打电话给我从角度服务构建的后端API,只要我有JWT令牌就可以正常工作。 但是当谈到测试时,我只想在我的组件测试中嘲笑服务。
服务
@Injectable()
export class ItemService {
constructor(private _http: AuthHttp, private _store: Store<AppStore>) {}
items(): Observable<any[]> {
return this._http.get(ENDPOINT_ITEMS, {withCredentials: false})
.map((response: Response) => response.json().data);
}
}
组件
@Component({
selector: 'items',
template: require('./items.component.html'),
providers: [ItemService]
})
export class ItemsComponent {
items: Observable<Array<Object>>;
constructor(private _service: ItemService) {}
ngOnInit(): any {
this.items = this._service.items();
}
}
组件测试
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ ItemsComponent ],
providers: [
{ provide: ItemService, useValue: itemsMock },
]
});
fixture = TestBed.createComponent(ItemsComponent);
itemService = fixture.debugElement.injector.get(ItemService)
我想我的嘲笑ItemService返回useValue,itemsMock,对我,所以我可以测试我的html标签确实有正确的数据。 但它看起来并不像我的服务被嘲笑的权利?
收到此错误:
Error: Error in ./ItemsComponent class ItemsComponent_Host - inline template:0:0 caused by: No provider for AuthHttp! in karma.entry.js (line 17341)
非常感谢!更进一步,它的工作,时间来重构! – mertje