angular - Angular .pipe and .subscribe undefined when selecting from ngrx store in unit tests
问题描述
I had some previously working unit tests and at some point they stopped working.
Whenever I select an observable from the ngrx store like so:
const observable = this.store.select(isAuthenticated);
and then attempt to call .pipe or .subscribe, these functions are undefined.
observable.subscribe(authenticated => {});
TypeError: observable.subscribe is not a function
or
TypeError: _this.store.select(...).pipe is not a function
I am importing the StoreModule and adding the appropriate model. These components work fine when not testing.
Edit: I use this MockStore function to add data to the store to select. I suspect the problem is in the map function as this changed with rxjs 6. the store selection is returning a map function instead of an observable.
import { TestBed } from '@angular/core/testing';
import { Store } from '@ngrx/store';
import { BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators';
export function MockStore(mockStore: any) {
const mockData: BehaviorSubject<Object> = new BehaviorSubject<Object>(mockStore);
const store = TestBed.get(Store);
const storeSpy = spyOn(store, 'select').and.callFake((fn) => {
return map.call(mockData, fn);
});
return storeSpy;
}
export function MockDispatch() {
const store = TestBed.get(Store);
const dispatchSpy = spyOn(store, 'dispatch').and.callFake(() => { return; });
return dispatchSpy;
}
解决方案
原来我是从错误的区域导入地图
改变:
import { map } from 'rxjs/operators';
至:
import { map } from 'rxjs/operator/map';
一切又开始工作了。
推荐阅读
- java - Shopify API 订单创建账单地址验证失败
- php - Laravel 将 mulitselect 数组结果转换为字符串
- html - 如果子项溢出父项,则调整子项的大小
- html - h1 在 Firefox 上的网格布局中具有空大小
- android - 应用程序发布到 Google Playstore 后,Firebase 消息不起作用
- sql-server - SQL Server 数据库中未插入的值
- php - 使用 PDO 连接添加到新表的问题
- java - 我如何在 10 秒内实时获取蓝牙 rssi 10 次
- google-chrome-extension - 如何在 Chrome 的扩展中包含库
- java - Kafka Stream Scala API 性能缓慢