Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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
Reactjs 组件中使用的酶模拟utils功能_Reactjs_Unit Testing_Jestjs_Enzyme - Fatal编程技术网

Reactjs 组件中使用的酶模拟utils功能

Reactjs 组件中使用的酶模拟utils功能,reactjs,unit-testing,jestjs,enzyme,Reactjs,Unit Testing,Jestjs,Enzyme,在我的React应用程序中,我想测试一个从另一个文件调用utils函数的组件。 在特定的情况下,我确实想模拟这个结果。我怎样才能用酶和笑话做到这一点 代码如下: // MyComponent import React from 'react'; import { getCalculatedValue } from '../utils'; export class MyComponent extends React.Component { constructor(props) { c

在我的React应用程序中,我想测试一个从另一个文件调用utils函数的组件。 在特定的情况下,我确实想模拟这个结果。我怎样才能用酶和笑话做到这一点

代码如下:

// MyComponent
import React from 'react';
import { getCalculatedValue } from '../utils';

export class MyComponent extends React.Component {
  constructor(props) {
    const calculatedValue = getCalculatedValue();
    // ...
  }
  // ...
}
以及测试:

// MyComponent test
describe('MyComponent ', () => {
  it('should be...', () => {
    const myComponent= shallow(
        <MyComponent />
    )
    expect(myComponent.find('....').length).toBeDefined()
  })
})
//MyComponent测试
描述('MyComponent',()=>{
它('应该是…',()=>{
常量myComponent=浅(
)
expect(myComponent.find('..').length.toBeDefined()
})
})

我想要模拟的方法是
getCalculatedValue

您可以在测试文件中执行以下操作:

getCalculatedValue = jest.fn().mockReturnValue(YOUR_RETURN_VALUE);
现在,当您使用
shallow
创建组件树时,
getCalculatedValue
将始终返回您的返回值

  • 你也可以

  • 如果您在utils.js中有多个函数,并且希望模拟所有函数,我建议您遵循以下文档:。只需调用
    jest.mock('path/to/utils')


请遵循最适合您的用例的操作。

您只需在测试文件中执行以下操作:

getCalculatedValue = jest.fn().mockReturnValue(YOUR_RETURN_VALUE);
现在,当您使用
shallow
创建组件树时,
getCalculatedValue
将始终返回您的返回值

  • 你也可以

  • 如果您在utils.js中有多个函数,并且希望模拟所有函数,我建议您遵循以下文档:。只需调用
    jest.mock('path/to/utils')


请遵循最适合您的用例的操作。

您可以使用
jest.mock
像这样模拟模块,请注意路径
'../utils'
与测试文件相关:

jest.mock('../utils', ()=>({getCalculatedValue: ()=> 'someValue'}))
如果需要为
getCalculatedValue
使用不同的返回值模拟模块,则需要使用jest.fn()模拟模块,将其导入测试并更改测试中的实现:

jest.mock('../utils', ()=>({getCalculatedValue: jest.fn()}))
import { getCalculatedValue } from '../utils';


it('test1',()=>{
   getCalculatedValue. mockReturnValue('someValue')
})


it('test2',()=>{
   getCalculatedValue. mockReturnValue('anotherValue')
})

您可以使用
jest.mock
像这样模拟模块,注意路径
'../utils'
与测试文件相关:

jest.mock('../utils', ()=>({getCalculatedValue: ()=> 'someValue'}))
如果需要为
getCalculatedValue
使用不同的返回值模拟模块,则需要使用jest.fn()模拟模块,将其导入测试并更改测试中的实现:

jest.mock('../utils', ()=>({getCalculatedValue: jest.fn()}))
import { getCalculatedValue } from '../utils';


it('test1',()=>{
   getCalculatedValue. mockReturnValue('someValue')
})


it('test2',()=>{
   getCalculatedValue. mockReturnValue('anotherValue')
})