[英]How to pass the store in the constructor in a typescript class?
I have this class sample that is using ngrx store an other services in the constructor, What is the best way to pass the store in the constructor when I am testing this class in jest?我有这个 class 示例,它在构造函数中使用 ngrx 存储其他服务,当我开玩笑地测试这个 class 时,在构造函数中传递存储的最佳方法是什么?
This class is not a component or injectable.这个 class 不是组件或注射剂。
export class Sample {
private users;
constructor(
private readonly userService: UserService,
private readonly store: Store
) { }
public setUsersDataFromStore() {
this.store.select(selectUsers).pipe(
filter((user) => !!users),
tap((users => (this.users = users)))
).subscribe();
}
}
Unit test.单元测试。
fit('should Sample Object defined', () => {
const sampleObj = new Sample(
mockUserService any,
initialState as any
);
sampleObj.setUsersDataFromStore();
expect(sampleObj.users).toBeDefined();
});
But the unit test is failing the selector is not getting the data from the initial state from the constructor, the initial state is a simple object, also I tried with the object in an observable of({initialState}), what is the best option to fix?但是单元测试失败,选择器没有从构造函数中获取初始 state 的数据,初始 state 是一个简单的 object,我也尝试在 observable of({initialState}) 中使用 object,最好的选择是什么修理?
You can use getMockStore
to create a mocked instance of the store, I briefly touch on it on this subject in my blog post Testing an NgRx project .您可以使用
getMockStore
创建商店的模拟实例,我在我的博客文章测试 NgRx 项目中简要介绍了这个主题。
new Sample(
mockUserService,
getMockStore({
initialState,
});
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.