作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 NgRx 实现有一个智能组件测试,如下所示:
describe( 'Component', () => {
let store: MockStore<State>;
beforeEach( async( () => {
TestBed.configureTestingModule( {
/* ... */
providers: [
provideMockStore( { initialState: fromReducer.initialState } )
]
} ).compileComponents();
store = TestBed.get<Store<State>>( Store );
} ) );
it( 'should load items in #ngOnInit', () => {
store.setState( {
item: {
...fromReducer.initialState,
entities: { [item.id]: item },
},
otherFeature: null,
otherFeature: null,
otherFeature: null
} );
component.items$.subscribe( items =>
store.select( ItemStoreSelectors.selectItems ).subscribe( fromStore => expect( items ).toEqual( fromStore ) )
);
} );
});
provideMockStore
和
setState
模拟我的 NgRx 状态。这样一切正常,但我真的不喜欢它的这一部分:
store.setState( {
item: {
...fromReducer.initialState,
entities: { [item.id]: item },
},
otherFeature: null,
otherFeature: null,
otherFeature: null
} );
setState
功能。否则 Typescript 会抛出错误。
store.setState( {
...fromReducer.initialState,
entities: { [item.id]: item },
} );
provideMockStore
的文档对于状态的特定切片
here .
最佳答案
我建议在这里采取不同的方法。你应该做的是独立地对你的选择器和组件进行单元测试。
NgRx 8 允许 mock selectors .在您的组件中使用它来验证组件的逻辑是否正确。
然后,单独对您的选择器进行单元测试,以验证您的选择器是否按预期工作。
通过这种方式,测试是独立的,而不是脆弱的,并且是真正的单元测试。
编辑:我链接的文档是官方的,但是模拟选择器的另一种方法是:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideMockStore({
selectors: [
{
selector: yourSelectorNameHere,
value: someMockValueHere
}
]
})
]
});
关于angular - NgRx 8 测试 provideMockStore - 状态切片的 setState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57287487/
使用 provide mockStore 时,我的功能/选择器函数只会将状态视为未定义。 因此,我只能模拟选择器,因此无法执行更完整的集成测试。 选择器是否不应该看到由模拟商店提供的状态,或者这应该有
使用 provide mockStore 时,我的功能/选择器函数只会将状态视为未定义。 因此,我只能模拟选择器,因此无法执行更完整的集成测试。 选择器是否不应该看到由模拟商店提供的状态,或者这应该有
我的 NgRx 实现有一个智能组件测试,如下所示: describe( 'Component', () => { let store: MockStore; beforeEach( async
我是一名优秀的程序员,十分优秀!