1
websocketインターフェイスとのやりとりを、自分のライブラリに抽象化しました。このライブラリは、アプリケーションを実装するためのシングルトンサービスを提供します。ライブラリに沿ってサービススタブをエクスポートするときの循環依存性
これで、このライブラリに依存するコンポーネントをテストしようとしています。
imports (...)
class MessageServiceStub {
private testData: WebsocketMessage =
{
'raw': new MessageEvent('type'),
.....
};
public getMessageStream =(): any => {
return Observable.of(this.testData);
}
public getStateStream =(): any => {
return Observable.of(this.testData);
}
}
describe('LandingComponent',() => {
let component: LandingComponent;
let fixture: ComponentFixture<LandingComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [LandingComponent],
imports: [ClarityModule.forRoot(), ReactiveFormsModule]
}).overrideComponent(LandingComponent, {
set: {
providers: [
{ provide: MessageService, useClass: MessageServiceStub },
]
}
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LandingComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create',() => {
expect(component).toBeTruthy();
});
});
テストファイルを一般化し、小さな独立テストに重点を置くために、ライブラリはMessageServiceStubもエクスポートします。しかし、.specからMessageServiceStubを削除し、ライブラリからインポートすると、代わりに次のエラーが発生します。
Error: Invalid provider for MessageService. useClass cannot be undefined.
Usually it happens when:
1. There's a circular dependency (might be caused by using index.ts (barrel) files).
2. Class was used before it was declared. Use forwardRef in this case.
私は運なし(forwardRef'ing MessageServiceStubを含む)は、複数の可能な解決策に見てきました。エラーメッセージは十分に明白なようですが、その解決策はありません。