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
Unit testing 如何承诺在使用Sinon.js一段时间后检查函数是否被调用_Unit Testing_Reactjs_Ecmascript 6_Sinon_Jestjs - Fatal编程技术网

Unit testing 如何承诺在使用Sinon.js一段时间后检查函数是否被调用

Unit testing 如何承诺在使用Sinon.js一段时间后检查函数是否被调用,unit-testing,reactjs,ecmascript-6,sinon,jestjs,Unit Testing,Reactjs,Ecmascript 6,Sinon,Jestjs,我正在用Jest和Ezyme为React.js应用程序编写测试。 我用来窥探React组件的成员函数。 其中一个问题是如何通过RESTAPI调用测试组件是否成功重定向 class MyComponent extends Component { ... componentWillReceiveProps(props) { ... if (this.props.phase === 'LOADING' && props.phase === 'SUCCESS'

我正在用Jest和Ezyme为React.js应用程序编写测试。
我用来窥探React组件的成员函数。
其中一个问题是如何通过RESTAPI调用测试组件是否成功重定向

class MyComponent extends Component {
  ...
  componentWillReceiveProps(props) {
     ...
     if (this.props.phase === 'LOADING' && props.phase === 'SUCCESS') {
        // redirect codes here
     }
  }
  handleClickMyButton() {
    dispatch(action)
  }
我的方法是

it ('Should be redirected', async () => {
  const spyPromise = ? // spy promise to componentWillReceiveProps will be called with certain parameters.
  const myComponentWrapper = wrapper.find(MyComponent)
  // simulate click event codes here

  const result = await spyPromise
  // check if it is redirected via history object

})

但是我不知道如何在这里进行
spyPromise

也许您需要添加更多的代码来解释您想要在这里进行测试的内容和原因。我也不会监视React组件的内部方法,而是监视进行重定向的代码。因此,如果您更改
document.location.href
只需检查它是否是使用
expect(global.document.location.href).toBe(“”)
设置的。此外,不必使用sinon,因为jest内置了大部分功能。我对
this.context.router.tansitito
没有问题,但我不确定如何使
spyPromise
等待REST api调用完成。从您的示例中,不清楚在哪里使用
spyPromise
const result=wait spyPromise
我正在等待REST api调用完成<代码>spy promise to componentWillReceiveProps将使用某些参数调用。这不是它的工作方式。通常,您会使用已解析的承诺来模拟REST调用,并将其与
wait
一起使用。您的组件应该经过设计,这样就可以将承诺传递到您的组件中,或者可以模拟用于进行调用的模块。仅仅在外部创建一个不与你的模块交互的承诺是没有效果的。