首页 > 解决方案 > Sinon - 如何使调用的函数间接返回某些内容

问题描述

我一直在努力研究如何模拟一个函数,以便我可以从该函数返回一个假值。

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

完整的代码非常复杂,这就是为什么我做了一个小样本来说明我的意思。首先我有这个功能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功能没那么难。它只是返回一个包含一些静态值和一些动态值的对象。

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 方法的返回值,以便它返回如下内容:

{
  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 函数的返回值的正确方向?一个例子将受到高度赞赏。

标签: javascriptnode.jsunit-testingmockingsinon

解决方案


你打电话的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. 在这种情况下,您可能还 stub doARequest。然后你可以用sinon.calledWith(doARequestStubb, fakeParam). 您可以doARequestStubb通过承诺代码不会中断来解决。


推荐阅读