Javascript 如何使用浅层(酶)模拟从render方法调用的方法

Javascript 如何使用浅层(酶)模拟从render方法调用的方法,javascript,testing,enzyme,Javascript,Testing,Enzyme,我需要模拟getZvalue,所以,当我基于z值进行浅层渲染时,我将尝试渲染不同的内容。我如何测试它。下面是示例代码。 我可以用这个方法返回一个值吗 class AbcComponent extends React.Component{ render(){ const z= this.getZValue(); return <div>{z}</div> } getZValue(){ //some calculations

我需要模拟getZvalue,所以,当我基于z值进行浅层渲染时,我将尝试渲染不同的内容。我如何测试它。下面是示例代码。 我可以用这个方法返回一个值吗

class AbcComponent extends React.Component{
  render(){
    const z= this.getZValue();
    return <div>{z}</div>

  }

  getZValue(){
    //some calculations 
    }
  }


describe('AbcComponent',()=>{
  it('Test AbcComponent',()=>{
    const wrapper= shallow<AbcComponent/>
  })
})
类AbcComponent扩展了React.Component{ render(){ const z=this.getZValue(); 返回{z} } getZValue(){ //一些计算 } } 描述('AbcComponent',()=>{ 它('testabccomponent',()=>{ 常数包装=浅 }) }) 这个怎么样

import { spy } from 'sinon';
describe('AbcComponent',()=> {
  it('Test AbcComponent',()=> {
    spy(AbcComponent.prototype, "getZValue");
    const wrapper= shallow<AbcComponent/>
    expect(AbcComponent.prototype.getZValue.callCount).to.equal(1);
    AbcComponent.prototype.getZValue.restore();
  })
})
从'sinon'导入{spy};
描述('AbcComponent',()=>{
它('testabccomponent',()=>{
spy(AbcComponent.prototype,“getZValue”);
常数包装=浅
expect(AbcComponent.prototype.getZValue.callCount).to.equal(1);
AbcComponent.prototype.getZValue.restore();
})
})
除此之外,还可以使用以下返回值进行测试:

   import { stub } from 'sinon';
    describe('AbcComponent',()=> {
      it('Test AbcComponent',()=> {
        stub(AbcComponent.prototype, "getZValue").returns(10);
        const wrapper= shallow<AbcComponent/>
        expect(AbcComponent.prototype.getZValue.callCount).to.equal(1);
        AbcComponent.prototype.getZValue.restore();
      })
    })
从'sinon'导入{stub};
描述('AbcComponent',()=>{
它('testabccomponent',()=>{
存根(AbcComponent.prototype,“getZValue”)。返回(10);
常数包装=浅
expect(AbcComponent.prototype.getZValue.callCount).to.equal(1);
AbcComponent.prototype.getZValue.restore();
})
})