Sinon - 如何创建一个间接调用的函数 return

Sinon - How to make a function thats called indirectly return something

我一直在努力研究如何模拟一个函数,以便我可以 return 从那个函数中得到一个假值。

我有一个可以进行 api 调用的简单脚本,但是这个 api 调用有两个参数。一个参数是通过父函数的参数提供的,另一个是通过调用另一个函数提供的。这个函数的 return 值是我需要模拟的值。

完整的代码相当复杂,这就是为什么我做了一个小样本来说明我的意思。首先我有函数makeTheCall。在该函数中,我调用了一个名为 setParameters.

的函数
const setParams = require('setParams.js');

module.exports.makeTheCall = (event) => {
  const params = setParams('GET', '/todos/1');
  const postData = {
    name: event.name,
    location: event.location
  }

  console.log(params); //dynamic params 'method' and 'callpath' are both undefined here (should be 'GET' and '/todos/1')

  return doARequest(params, postData).then((result) => {
    return result;
  }).catch((error) => {
    return error;
  })
}

setParams函数没有那么难。它只是 return 一个包含一些静态值和一些动态值的对象。

module.exports.setParams = (method, callPath) => {
  return {
    host: 'jsonplaceholder.typicode.com',
    port: 433,
    method: method,
    path: callPath
  }
}

现在,有趣的部分开始发挥作用了。编写简单测试时,调用无法通过。当然,这是因为它无法解析动态值 methodcallPath.

const makeTheCall = require('makeTheCall.js');

it('runs a happy flow scenario', () => {
  const event = {
    name: 'John Doe',
    location: 'Somewhere'
  }

  return makeTheCall(event)
    .then(response => {
      //Do some usefull testing here
    });
});

我的问题是如何模拟 setParams 方法的 return 值,以便它 return 类似于:

{
  host: 'jsonplaceholder.typicode.com',
  port: 433,
  method: 'GET',
  path: '/todos/1'
}

这样我就可以在我的测试中调用我的 API 调用而不会导致错误。我一直在研究使用 sinon 进行模拟,尤其是 sinon 存根,例如:

const params = setParams('GET', '/todos/1');
sinon.stub(params).returns({
  host: 'jsonplaceholder.typicode.com',
  port: 433,
  method: 'GET',
  path: '/todos/1'
});

但是我想我忽略了一些东西,因为这不起作用。文档很好,但经过几个小时的努力和尝试后,我开始感到有点迷茫。

谁知道/可以为我指明正确的方向,了解如何模拟 setParams 函数的 return 值?一个例子将不胜感激。

你叫 sinon.stub 不对。 stub() 需要一个对象和一个作为该对象的 属性 的函数。如果您导入:

 const setParams = require('setParams.js');

然后 setParams 将成为 modules.export 对象,而 setParams 将成为 属性,因此您可以将其存根为:

let fakeParam = {
    host: 'jsonplaceholder.typicode.com',
    port: 433,
    method: 'GET',
    path: '/todos/1'
}

let paramStub = sinon.stub(params, 'setParams').returns(fakeParam)

从更广泛的角度来看,您并不清楚您要测试的是什么。通过单元测试,您通常会尝试将所有内容简化为您想要断言的一件小事。因此,在这种情况下,您可能想断言,当您调用 makeTheCall 时,doARequest 是使用从 setParams 返回的参数调用的。在那种情况下,您也可以存根 doARequest。然后你可以用 sinon.calledWith(doARequestStubb, fakeParam) 断言。您可以 doARequestStubb 解决并承诺代码不会中断。