[英]Test debounced Vue method with Mocha Chai and Sinon
If I have a Vue component like: 如果我有Vue组件,例如:
import lodash from 'lodash';
export default {
//...data, props etc
methods: {
someFunction: lodash.debounce(function debouncedsomeFunction() {
return 'test';
}, 200)
}
};
How would I go about testing someFunction
with Mocha + Chai + Sinon? 我将如何使用Mocha + Chai + Sinon测试
someFunction
?
There are several potential approaches, but it sounds like you want to mock lodash. 有几种潜在的方法,但听起来像是要模拟lodash。 I'll list the steps linearly for ease of presentation, but you'll probably want to distribute them appropriately within your test blocks and hooks.
为了便于演示,我将线性列出这些步骤,但是您可能希望将它们适当地分布在测试块和挂钩中。 I'm also assuming the only call to
debounce
is the method in question. 我还假设
debounce
的唯一调用是有问题的方法。 If not, adjust the following accordingly. 如果不是,请进行以下调整。
import * as Lodash from "lodash";
const debounce = sinon.stub(Lodash, "debounce").returns(() => {});
// create your component
expect(debounce.calledOnce).to.equal(true);
expect(debounce.firstCall.args[0]).to.be.a("function");
expect(debounce.firstCall.args[0]()).to.equal("test");
Lodash.debounce.restore();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.