测试 ngrx:调度操作并期待商店
testing ngrx: dispach action and expect the store
是否可以在没有测试效果、缩减器、选择器的情况下,在测试中分派一个动作并期望存储?我真的找不到一个好的例子,所以我想知道哪个是测试 ngrx 的最佳实践。
有什么想法吗?
"store" 是代表整个 NgRx 系统的术语。减速器是产生状态的东西。我认为您想测试您的操作如何产生状态。为此,您需要专注于您的 "state creator",即您的 reducer
reducer 是一个函数。考虑这个简单的例子,我激活了一个 "product" 我想测试当 ActivateProduct 动作出现时,我的加载状态设置为 true
export function productActivationReducer(state = initialState, action: ProductActivationActions): ProductActivationState {
switch (action.type) {
case ProductActivationActionTypes.ActivateProductSuccess:
return {
isLoading: true,
error: null,
data: action.payload,
};
default:
return state;
}
}
你的reducer是一个函数。可以调用该函数我将创建一个测试来调用该函数并测试返回的状态。
import { productActivationReducer, initialState } from './product-activation.reducer';
describe('ActivateProductSuccess', () => {
it('should set the next state', () => {
action = new ActivateProductSuccess({ foo: 'bar'});
result = productActivationReducer(initialState, action);
expect(result.data).toEqual({ foo: 'bar'});
expect(result.error).toBeNull();
expect(result.loading).toBe(true);
});
});
是否可以在没有测试效果、缩减器、选择器的情况下,在测试中分派一个动作并期望存储?我真的找不到一个好的例子,所以我想知道哪个是测试 ngrx 的最佳实践。
有什么想法吗?
"store" 是代表整个 NgRx 系统的术语。减速器是产生状态的东西。我认为您想测试您的操作如何产生状态。为此,您需要专注于您的 "state creator",即您的 reducer
reducer 是一个函数。考虑这个简单的例子,我激活了一个 "product" 我想测试当 ActivateProduct 动作出现时,我的加载状态设置为 true
export function productActivationReducer(state = initialState, action: ProductActivationActions): ProductActivationState {
switch (action.type) {
case ProductActivationActionTypes.ActivateProductSuccess:
return {
isLoading: true,
error: null,
data: action.payload,
};
default:
return state;
}
}
你的reducer是一个函数。可以调用该函数我将创建一个测试来调用该函数并测试返回的状态。
import { productActivationReducer, initialState } from './product-activation.reducer';
describe('ActivateProductSuccess', () => {
it('should set the next state', () => {
action = new ActivateProductSuccess({ foo: 'bar'});
result = productActivationReducer(initialState, action);
expect(result.data).toEqual({ foo: 'bar'});
expect(result.error).toBeNull();
expect(result.loading).toBe(true);
});
});