【问题标题】:How can we test the return type of method by using sinon.spy?我们如何使用 sinon.spy 测试方法的返回类型?
【发布时间】:2017-03-30 06:19:40
【问题描述】:

我们正在使用 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测试action的返回类型?请帮助我。

【问题讨论】:

    标签: reactjs sinon sinon-chai


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      相关资源
      最近更新 更多