Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 如何模拟库函数(测试redux操作创建者)_Unit Testing_Mocha.js_Redux - Fatal编程技术网

Unit testing 如何模拟库函数(测试redux操作创建者)

Unit testing 如何模拟库函数(测试redux操作创建者),unit-testing,mocha.js,redux,Unit Testing,Mocha.js,Redux,我在“Redux世界”中有个例子 我有action creator(简单功能): 我对它进行了测试(): 当然,我有不同的v4 ID: 在我的测试中直接使用v4(更改:id:'fake-v4-id'->id:v4())并不能解决问题。ID将是不同的 我需要什么?模仿,超越?还是别的什么?我该怎么做 我的测试框架是Mocha,断言库是-Chai。Recipie是: 安装 在babelrc中设置testenv(类似这样的东西) { “预设”:[“es2015”、“第0阶段”、“反应”、“反应hmr

我在“Redux世界”中有个例子

我有action creator(简单功能):

我对它进行了测试():

当然,我有不同的v4 ID:

在我的测试中直接使用v4(更改:
id:'fake-v4-id'->id:v4()
)并不能解决问题。ID将是不同的

我需要什么?模仿,超越?还是别的什么?我该怎么做

我的测试框架是Mocha,断言库是-Chai。

Recipie是:

  • 安装

  • 在babelrc中设置testenv(类似这样的东西)

    { “预设”:[“es2015”、“第0阶段”、“反应”、“反应hmre”], “环境”:{ “测试”:{ “插件”:[“重新布线”] } } }

  • 安装babel cli(如果尚未安装)

  • 在package.json中键入test命令,如下所示:

    NODE_ENV=test babel NODE_modules/.bin/_mocha-r——递归

  • *为什么是摩卡咖啡?()

    好的,设置就绪。现在编辑测试文件:

    import { __RewireAPI__, addTodo } from '../actions'
    ...
    describe('Todo actions', () => {
    
      before(() => {
        __RewireAPI__.__Rewire__('v4', () => 'fake-v4-id')
      })
    
      it('should create an action for add todo', () => {
        const text = 'test v4 call'
        const expectedAction = {
          type: 'ADD_TODO',
          text,
          id: 'fake-v4-id',
        }
    
        expect(addTodo(text)).toEqual(expectedAction)
      })
    
    })
    

    运行
    npm测试

    该示例不应该在之后包含一个调用
    ComponentToTest.\uuuuu重置依赖关系('v4')的
    ?@machineghost,嗯。。。可能是。看起来很棒,但我不知道这件事。
    
    import { addTodo } from '../actions'
    ...
    it('should create an action for add todo', () => {
        const text = 'test v4 call'
        const expectedAction = {
          type: 'ADD_TODO',
          text,
          id: 'fake-v4-id', // ???
        }
    
        expect(addTodo(text).toEqual(expectedAction)
      })
    
    import { __RewireAPI__, addTodo } from '../actions'
    ...
    describe('Todo actions', () => {
    
      before(() => {
        __RewireAPI__.__Rewire__('v4', () => 'fake-v4-id')
      })
    
      it('should create an action for add todo', () => {
        const text = 'test v4 call'
        const expectedAction = {
          type: 'ADD_TODO',
          text,
          id: 'fake-v4-id',
        }
    
        expect(addTodo(text)).toEqual(expectedAction)
      })
    
    })