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
Testing 使用react组件和上下文进行测试:返回空对象_Testing_Reactjs_Mocha.js_Enzyme - Fatal编程技术网

Testing 使用react组件和上下文进行测试:返回空对象

Testing 使用react组件和上下文进行测试:返回空对象,testing,reactjs,mocha.js,enzyme,Testing,Reactjs,Mocha.js,Enzyme,我试图在一个组件上用酶做一个虚拟测试。测试即将检查上下文。尽管我编写的代码与Ezyme的文档相同,但上下文总是空的 import React from 'react'; import { shallow } from 'enzyme'; import Overlay from '../../../../app/components/Overlay/Overlay'; describe('<Overlay />', () => { it.only('return a co

我试图在一个组件上用酶做一个虚拟测试。测试即将检查上下文。尽管我编写的代码与Ezyme的文档相同,但上下文总是空的

import React from 'react';
import { shallow } from 'enzyme';
import Overlay from '../../../../app/components/Overlay/Overlay';


describe('<Overlay />', () => {
  it.only('return a context', () => {
    const wrapper = shallow(<Overlay />, { context: { foo: 10 } });
    console.log(wrapper.context());
    // expect(wrapper.context().foo).to.equal(10);
  });
})
从“React”导入React;
从“酶”导入{shall};
从“../../../../app/components/Overlay/Overlay”导入覆盖;
描述(“”,()=>{
it.only('返回上下文',()=>{
const wrapper=shallow(,{context:{foo:10}});
log(wrapper.context());
//expect(wrapper.context().foo).to.equal(10);
});
})
测试的输出为:

<Overlay />
{}
✓ return a context

{}
✓ 返回上下文

我错在哪里?

由于未给出
覆盖
组件的详细信息,我假设上下文未在其中使用(请检查
childContextTypes
getChildContext
是否正确定义)

例如,请参阅react中的上下文说明

我举了同样的例子来进行测试

import React from 'react';

export default  class Button extends React.Component {
  render() {
    return (
      <button style={{ background: this.context.color }}>
        {this.props.children}
      </button>
    );
  }
}

Button.contextTypes = {
  color: React.PropTypes.string,
};

class Message extends React.Component {
  render() {
    return (
      <div>
        {this.props.text} <Button>Delete</Button>
      </div>
    );
  }
}

class MessageList extends React.Component {
  getChildContext() {
    return { color: 'purple' };
  }

  render() {
    const children = this.props.messages.map((message) =>
      <Message text={message.text} />
    );
    return <div>{children}</div>;
  }
}

MessageList.childContextTypes = {
  color: React.PropTypes.string,
};

✓ 上下文断言
1次通过(214ms)
这将正确地断言它

import React from 'react';
import { shallow } from 'enzyme';
import { expect } from 'chai';
import Button from '../../src/components/SampleComp';

describe.only('<Button />', () => {
  it('assert for context', () => {
    const wrapper = shallow(
      <Button />,
      { context: { color: 'red' } }
    );
    expect(wrapper.context().color).to.equal('red');
    expect(wrapper.context('color')).to.equal('red');
  });
});
 <Button />
    ✓ assert for context


  1 passing (214ms)