【发布时间】:2017-05-11 20:40:13
【问题描述】:
我正在尝试使用 Mocha 和 Sinon.js 存根 Stripe 模块进行单元测试。
我需要这样的条纹:
const stripe = require('stripe');
const stubbedStripeClient = stripe.Stripe('test');
在我的测试的根源(在我的顶级describe())我有这个:
before('stub root', () => {
sinon.stub(stripe, 'Stripe').returns(stubbedStripeClient);
});
然后,在我实际上会调用 Stripe 方法的 describe() 块中,我有这个 before() 钩子:
let stub;
before('stub', () => {
console.log(typeof stubbedStripeClient.customers.create);
stub = sinon.stub(stubbedStripeClient.customers, 'create', ({id: 'a-stripe-customer-id'}));
});
这是我不明白会发生什么的地方。钩子中的第一行 (console.log) 输出:
功能
第二行抛出这个异常:
TypeError: Attempted to wrap undefined property create as function
这怎么可能?怎么可能在一行上是一个函数,而在下一行却是未定义的?
我查看了 Sinon.js 源代码,并执行了此检查 here。如果我再查看他们的isFunction 函数,它会执行与我在console.log 中相同的检查。我很困惑。
【问题讨论】:
-
一条线索,存根调用的第三个参数不是函数而是对象。 Looks like 它必须是一个函数。
-
哇哦。我实际上希望这不是解决方案,但它现在确实有效......我真的需要对此进行外部查看,我只是忘记了
() =>来声明一个函数,所以正如你所说,我是通过一个普通的对象作为第三个参数。但是错误消息不是很清楚:/非常感谢@Will
标签: javascript node.js sinon