我们如何使用sinon.spy测试方法的返回类型?

时间:2017-03-30 06:19:41

标签: reactjs sinon sinon-chai

我们正在使用sinon在reactjs应用程序中测试我们的api调用像这样: -

import * as Actions from 'routes/actions/Actions';
const requestAction = {
  RequestShell() { Actions.request(); },
};
 describe('testing for Actions', () => {
  it('check whether request() method call is happening properly or not', () => {
   const requestData = sinon.spy(requestAction, 'RequestShell');
   requestAction.RequestShell();
   sinon.assert.calledOnce(requestData);
   requestData.restore();
 });

现在我需要比较Actions.request()返回类型是否为Json对象。如何使用 sinon 测试操作的返回类型?请帮助我。

1 个答案:

答案 0 :(得分:1)

试试这个

<强> JS

 it('check whether request() method call is happening properly or not', () => {
    const requestData = sinon.spy(requestAction, 'RequestShell');
    requestAction.RequestShell();
    assert(requestData.calledOnce);
    requestAction.RequestShell.restore();
  });

请参阅此链接sinon spies

相关问题