首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在约束反应分量法上使用sinon?

如何在约束反应分量法上使用sinon?
EN

Stack Overflow用户
提问于 2017-02-04 00:06:44
回答 1查看 2.5K关注 0票数 0

我在使用绑定方法(https://babeljs.io/docs/plugins/transform-class-properties/)时遇到了一些困难。

我怎么能跟间谍交往?这里有一个要点:https://gist.github.com/stevens32/b5eee5cc1781a687be03bf80ce8425e0

其结果是:

代码语言:javascript
复制
bound method spying
  √ should be an instance of FormComponent
  should spy on boundChangeInput
    √ should have calledOnce prop on boundChangeInput from spy
    1) should have boundChangeInput.calledOnce true on simulated input change
    √ has the correct value
  should spy on notBoundChangeInput
    √ should have calledOnce prop on notBoundChangeInput from spy
    √ should have notBoundChangeInput.calledOnce true on simulated input change
    √ has the correct value

 6 passing (133ms)
  1 failing

  1) bound method spying should spy on boundChangeInput should have boundChangeInput.calledOnce true on
mulated input change:

  AssertionError: expected false to equal true
  + expected - actual

  -false
  +true
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-21 15:26:30

您可能需要首先创建组件的实例。试试这个:

代码语言:javascript
复制
describe('should have boundChangeInput.calledOnce true on simulated input change', function() {

  const node = mount(<FormComponent />)
  const component = wrapper.instance()

  let boundChangeSpy = sinon.spy(node, 'boundChangeInput')

  component.forceUpdate()
  wrapper.update()

  wrapper.find('input').at(0).simulate('change',{target:{value:'some value'}})

  expect(node.boundChangeSpy.calledOnce).to.equal(true)
})

来源:Test custom method on React component has been called, using Enzyme and Sinon

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42034967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档