Sinon存根函数与解构一起使用 [英] Sinon stub function used with destructuring

查看:70
本文介绍了Sinon存根函数与解构一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对当前正在测试的文件中使用的函数进行存根.像这样的解构需要此功能:

I wish to stub a function used in the file I'm currently testing. This function is required with a destructuring like this:

 const { theFunctionIWant } = require('path/to/module')

在测试时,永远不会调用存根,而是继续调用实函数. 但是,当我正常"要求(即不进行销毁)时

When testing, the stub is never called, and the real function proceed to be called. But when I require it 'normally' (i.e: without destructuring)

const myModule = require('path/to/module')

然后正确使用存根,并且一切正常

then the stub is correctly used and everything works fine

我认为这是由于解构的工作原理以及sinon存根对象属性而不是直接存入函数的事实.无论如何,如果您能为我提供一些见解,我将不胜感激!

I sense that it's because of how the destructuring works and the fact that sinon stub the object property and not the function directly. Anyhow if you can provide me some insights I will be grateful !

推荐答案

使用从依赖模块进行解构时,对模块方法进行存根的原因很简单,并且与绑定实际模块的时间有关功能参考.它与CommonJS模块(Sinon或Node本身)没有任何关系,因此我将以简单的JavaScript示例开始.

The reason stubbing the method of the module doesn't work when using destructuring from a dependant module is quite simple and has to do with the time you bind the actual function reference. It doesn't have anything to do with CommonJS modules, Sinon or Node per se, so I'll start out with simple javascript examples.

const stub = (o, method) => (o[method] = () => "I am a stub");

const obj = {
  methodFoo() {
    return "I am foo";
  }
};

// same as doing `const methodFoo = obj.methodFoo;`
const { methodFoo } = obj; // "import" using destructuring

console.log("obj.methodFoo(): ", obj.methodFoo());
console.log("methodFoo()", methodFoo());

console.log("Stubbing out method!");
stub(obj, "methodFoo");

console.log("obj.methodFoo: ", obj.methodFoo());
console.log("methodFoo()", methodFoo());

如果运行上面的示例,即使 您已将obj模块"的methodFoo属性存根, 直接绑定的引用仍然返回旧值!

If you run the example above, you will see that even though you have stubbed the methodFoo property of the obj "module", the directly bound reference still returns the old value!

这是因为,在进行存根时,您实际上是在分配 对象属性(此处为obj)的新值(函数).对此新值的新引用(使用obj.methodFoo)将打印新值, 但是如果您已经存储了对旧功能的引用,您将 调用旧函数时仍会获得旧的返回值.

This is because, when stubbing you are essentially assigning a new value (a function) to a property of an object (here: obj). New references to this new value (using obj.methodFoo) will print the new values, but if you have stored a reference to the old function you will still get the old return values when calling the old function.

这同样适用于您的原始问题.如果您在模块A中具有依赖项 在模块B中的函数foo store 上进行引用,然后 是否分配新值(例如存根)都没有关系 导出的值,因为您已经存储了对旧值的引用.

The same applies to your original problem. If you in module A have a dependency on a function foo in module B and store that reference, then it doesn't matter if you assign a new value (for instance a stub) to the exported value, since you already stored a reference to the old value.

本质上:

这将受到存根的影响

const A = require('./A');

function doFoo(){
    A.foo(); // will always evalute  A['foo']()
}

这不会受到存根的影响

const myFoo = require('./A').foo;

function doFoo(){
    myFoo(); // will just evalute to the original value of A.foo()
}

这篇关于Sinon存根函数与解构一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆