[英]How to test for user-specified time in setTimeout
Right now I have a jQuery plugin that adds some extra customization (around how to handle the return data in certain contexts) and polling a server for information and changes.现在我有一个 jQuery 插件,它添加了一些额外的自定义(关于如何在某些上下文中处理返回数据)并轮询服务器以获取信息和更改。 One of the required features is to allow the user set the polling interval.所需功能之一是允许用户设置轮询间隔。 setTimeout
(paired with recursion) is the function that is used for creating the constant poll; setTimeout
(与递归配对)是用于创建常量轮询的 function; this is also a requirement.这也是一个要求。
This is where I am stumped: along with the required functionality noted above, I also have to test if the user-specified value was actually used in the setTimeout.这就是我被难住的地方:除了上面提到的所需功能外,我还必须测试用户指定的值是否实际用于 setTimeout。 How would I go about doing this from a QUnit perspective?从 QUnit 的角度来看,我将如何 go 这样做?
I have already pondered straight timing the function with Date.getTime(), but the way things are currently set up this isn't an option.我已经考虑过使用 Date.getTime() 直接为 function 计时,但目前的设置方式不是一个选项。
Consider refactoring the code in a way that let's you override the method starting the timeout within your test, allowing you to verify that the correct value is passed, without actually starting the timeout.考虑以某种方式重构代码,让您在测试中覆盖启动超时的方法,允许您验证是否传递了正确的值,而无需实际启动超时。
If you need to actually call setTimeout, consider using sinon.js to mock setTimeout itself and control what it does instead.如果您需要实际调用 setTimeout,请考虑使用sinon.js来模拟 setTimeout 本身并控制它的作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.