Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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 如何在React测试中测试、模拟上下文?_Reactjs_Unit Testing_Jestjs_Enzyme - Fatal编程技术网

Reactjs 如何在React测试中测试、模拟上下文?

Reactjs 如何在React测试中测试、模拟上下文?,reactjs,unit-testing,jestjs,enzyme,Reactjs,Unit Testing,Jestjs,Enzyme,我有这样的代码: render () { ... const { exampleMethod, } = this.props; const { getPath } = this.context; const exampletData = exampleMethod(getPath()); ... } 在本例中,如何模拟、测试上下文?通常如何测试上下文?如果您使用的是酶,则可以通过传递一个选项来设置浅层渲染或完整dom渲染时的上下文: 例如: 如果要在节点上

我有这样的代码:

render () {
  ...

  const {
    exampleMethod,
  } = this.props;

  const { getPath } = this.context;
  const exampletData = exampleMethod(getPath());

  ...
}
在本例中,如何模拟、测试上下文?通常如何测试上下文?

如果您使用的是酶,则可以通过传递一个选项来设置浅层渲染或完整dom渲染时的上下文:

例如:

如果要在节点上测试上下文,可以使用来自Ezyme的方法。如果使用Ezyme,则可以通过传递选项设置浅层渲染或完全dom渲染时的上下文:

例如:

如果要在节点上测试上下文,可以使用来自Ezyme的方法

import { shallow, mount } from 'enzyme';

// ......

const testContext = { getPath: () => 'foo' };
const shallowWrapper = shallow(<SomeComponent />, { context: testContext });

// or 

const fullWrapper = mount(<SomeComponent />, { context: testContext });