简体   繁体   English

角度测试因takeWhile()而失败

[英]Angular tests fail with takeWhile()

I have a component which uses takeWhile() when subscribing to NgRx reducer with select . 我有使用组件takeWhile()订阅NGRX减速器时select As such: 因此:

    this.store.pipe(select(reducer.getProviders))
            .takeWhile( ... )
            .subscribe(providers => {
                ...
            });

And now I want to write tests for it. 现在我要为此编写测试。 At the moment it's pretty basic: 目前,这是非常基本的:

    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();
            }));
        });
    });

But when I run it, I get this error: 但是,当我运行它时,出现以下错误:

TypeError: this.store.pipe(...).takeWhile is not a function

I don't know how to import it! 我不知道如何导入! Can anyone help me with this? 谁能帮我这个?

need to import takewhile operator in component 需要在组件中导入Takewhile运算符

import 'rxjs/add/operator/takeWhile';

for rxjs 5.5 above use below statement 对于上面的rxjs 5.5使用下面的语句

import { takeWhile } from 'rxjs/operators';

takeWhile is a pipe-able operator and should be included inside the pipe: takeWhile是可管道操作的运算符,应包含在管道中:

this.store.pipe(
   select(reducer.getProviders),
   takeWhile( ... ) 
).subscribe(providers => {
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM