我想对一个组件进行单元测试。我正在使用 @ngrx/store 和 rxjs 来从组件更新我的存储区。为此,我订阅并取消订阅存储区。
我的单元测试失败,并显示以下错误:
TypeError: undefined is not an object (evaluating 'this.store.unsubscribe') in config/spec-bundle.js
我无法找到正确模拟我的存储区及其事件发射器的方法。
以下是相关代码:
app.component.ts:
ngOnDestroy() {
// unsubscribe the observable
this.userStore.unsubscribe();
}
app.component.spec.ts :
如何模拟存储器:
export class MockStore extends Subject<fromRoot.State> implements Store <fromRoot.State> {}
如何配置:
如何进行配置:
TestBed.configureTestingModule({
imports: [
FormsModule,
ReactiveFormsModule,
StoreModule.provideStore(reducer)
],
declarations: [LoginComponent],
providers: [
BaseRequestOptions,
MockBackend,
{ provide: AuthService, useValue: authServiceStub },
{ provide: LoginService, useValue: loginServiceStub },
{ provide: LoggerService, useValue: loggerServiceStub },
{
provide: Http, useFactory: (backend: ConnectionBackend,
defaultOptions: BaseRequestOptions) => {
return new Http(backend, defaultOptions);
}, deps: [MockBackend, BaseRequestOptions]
},
{ provide: Router, useClass: MockRouter }
]
})
告诉我如果你需要更多信息。提前感谢。