我有一个使用takeWhile()
订阅NgRx reducer时使用select
的组件。因此:
this.store.pipe(select(reducer.getProviders))
.takeWhile( ... )
.subscribe(providers => {
...
});
现在我要为此编写测试。目前,这是非常基本的:
import { StoreModule, Store, Action, select, combineReducers } from '@ngrx/store';
import { Subject } from 'rxjs/Subject';
import * as reducer from './../../reducers';
describe('ProviderComponent', () => {
let component: ProviderComponent;
let fixture: ComponentFixture<ProviderComponent>;
let store: Store<reducer.State>;
beforeEach(async(() => {
const actions = new Subject<Action>();
const states = new Subject<reducer.State>();
store = mockStore<reducer.State>({ actions, states });
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({
provider: combineReducers(reducer.reducers)
}),
...
],
declarations: [
ProviderComponent
],
providers: [
{ provide: Store, useValue: store },
{ provide: HAMMER_LOADER, useValue: () => new Promise(() => { return; }) }
]
})
fixture = TestBed.createComponent(ProviderComponent);
component = fixture.componentInstance;
component.providerCtrl = new FormControl();
spyOn(store, 'dispatch').and.callThrough();
}));
describe('When provider is rendered', () => {
beforeEach(() => {
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('store to be defined', async(() => {
expect(store).toBeDefined();
}));
});
});
但是当我运行它时,出现此错误:
TypeError: this.store.pipe(...).takeWhile is not a function
我不知道如何导入它! 有人可以帮我吗?
答案 0 :(得分:1)
需要在组件中导入Takewhile运算符
import 'rxjs/add/operator/takeWhile';
对于以上的rxjs 5.5,请在下面的语句中使用
import { takeWhile } from 'rxjs/operators';
答案 1 :(得分:1)
takeWhile是可管道运算符,应包含在管道中:
this.store.pipe(
select(reducer.getProviders),
takeWhile( ... )
).subscribe(providers => {
...
});