简体   繁体   English

@angular-redux/store:如何为 redux 商店编写单元测试?

[英]@angular-redux/store : How to write unit test for the redux store?

I want to mock the redux store and write tests against the redux-store directly to the store.我想模拟 redux 存储并将针对 redux-store 的测试直接写入存储。 I don't want any angular logic to come in between.我不希望任何 angular 逻辑介于两者之间。 Can somebody help?有人可以帮忙吗?

Since angular-redux is using plain redux inside you should be able to just invoke the reducer function itself.由于 angular-redux 在内部使用普通的 redux ,因此您应该能够只调用减速器 function 本身。 Without Angular, no mocking is needed.如果没有 Angular,则不需要 mocking。 Just pass a current state and a given action.只需传递当前的 state 和给定的操作。

// reducers.spec.ts
import {ticketsReducer} from './reducer.ts'

describe('Ticket Reducer Test', () => {

  it('should add one ticket', () => {
    // given
    const currentstate = 1;
    const action = { type: 'ADD_TICKET '};
    // when
    const state = ticketsReducer(state, action);
    // then
    expect(state).toBe(2);
  })

});

// reducer.ts
export const ticketsReducer: Reducer<number> = (state = 0, action: Action) => {
  switch (action.type) {
    case ADD_TICKET:
      return state + 1;
    case REMOVE_TICKET:
      return Math.max(0, state - 1);
  }
  return state;
};

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

相关问题 如何单元测试从 angular 10 中的“@angular-redux/store”中选择? - how to unit test select from "@angular-redux/store" in angular 10? 如何在angular-redux / store中初始化根存储以进行测试? - How to initialize root store in angular-redux/store for testing? 在Storybook中使用@ angular-redux / store测试Angular - Testing Angular with @angular-redux/store in Storybook 带有 angular-redux/store 的 angular Guard - angular guard with angular-redux/store 使用@ angular-redux / store测试@select装饰器 - Testing @select decorators with @angular-redux/store 测试 Angular 组件,该组件引入了一个派生自 @angular-redux/store 的“@select()”方法的 observable - Test Angular component that brings in an observable derived from @angular-redux/store's “@select()” method 在升级到9.0和角度7后修复angular-redux / store - Fix angular-redux/store after upgrade to 9.0 and angular 7 @angular-redux/store 尝试测试在用 @select() 装饰的属性上使用异步的组件 - 文档不存在 - @angular-redux/store trying to test component that uses async on a property decorated with @select() - documentation does not exist 如何使用angular-redux的redux-state-sync中间件? - How to use redux-state-sync middleware with angular-redux? Angular-Redux设计-数组选择器 - Angular-redux design - array selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM