酶:BrowserHistory功能测试失败

时间:2016-08-13 20:36:24

标签: unit-testing reactjs sinon enzyme sinon-chai

我对单元测试很新。我正在使用react + redux,我创建了一个NotFound页面/组件,我正在撰写单元测试。有一个onClick事件,我需要测试哪个失败。

404.jsx

const GenericNotFound = () => {
  const goBack = () => {
    browserHistory.goBack();
  };
  return (
    <section >
      <h1>Sorry. The requested URL is not found</h1>
      <a onClick={goBack}>Go back</a>
    </section>
  );
};

Test.js

 const wrapper = shallow(<GenericNotFound />);
  const browserHistory = {
    goBack: sinon.spy()
  };
  const onClick = wrapper.find('a').props().onClick;
  onClick();
  expect(browserHistory.goBack).to.have.been.called;

即使这样,它也会给我一个错误Cannot read property 'goBack' of undefined

提前谢谢。

2 个答案:

答案 0 :(得分:1)

通过关注@anoop回答:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

# Init Form
$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 200
$Form.Text = "**OSP Installation in Progress**"

# Init ProgressBar
$pbrTest = New-Object System.Windows.Forms.ProgressBar
$pbrTest.Maximum = 100
$pbrTest.Minimum = 0
$pbrTest.Location = new-object System.Drawing.Size(10,70)
$pbrTest.size = new-object System.Drawing.Size(967,10)
$i = 0
$Form.Controls.Add($pbrTest)

# Show Form
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

答案 1 :(得分:0)

根据评论更新,

一些变化

  1. 使用mount代替shallow
  2. const mount = mount(<GenericNotFound />);

    1. 使用spy,如下所示
    2. sinon.spy(browserHistory.prototype, 'goBack')