我可以“模拟"吗?PHPUnit 的时间?
...不知道'mock'是否是正确的词.
... not knowing if 'mock' is the right word.
无论如何,我有一个继承的代码库,我正在尝试为其编写一些基于时间的测试.尽量不要太模糊,代码与查看项目的历史记录并确定该项目现在是否基于时间阈值有关.
Anyway, I have an inherited code-base that I'm trying to write some tests for that are time-based. Trying not to be too vague, the code is related to looking at the history of an item and determining if that item has now based a time threshold.
在某些时候,我还需要测试向该历史记录添加一些内容并检查阈值现在是否已更改(并且显然是正确的).
At some point I also need to test adding something to that history and checking that the threshold is now changed (and, obviously, correct).
我遇到的问题是我正在测试的部分代码正在使用对 time() 的调用,所以我发现很难确切地知道阈值时间应该是多少,基于事实我不太确定 time() 函数何时会被调用.
The problem that I'm hitting is that part of the code I'm testing is using calls to time() and so I'm finding it really hard to know exactly what the threshold time should be, based on the fact that I'm not quite sure exactly when that time() function is going to be called.
所以我的问题基本上是这样的:我有什么方法可以覆盖" time() 调用,或者以某种方式模拟"时间,以便我的测试在已知时间"内工作?
So my question is basically this: is there some way for me to 'override' the time() call, or somehow 'mock out' the time, such that my tests are working in a 'known time'?
或者我是否只需要接受这样一个事实,即我将不得不在我正在测试的代码中做一些事情,以某种方式允许我在需要时强制它使用特定时间?
Or do I just have to accept the fact that I'm going to have to do something in the code that I'm testing, to somehow allow me to force it to use a particular time if need be?
无论哪种方式,是否有任何通用做法"来开发对测试友好的时间敏感功能?
Either way, are there any 'common practices' for developing time-sensitive functionality that is test friendly?
我的部分问题也是,历史上发生的事情的时间会影响阈值.这是我的部分问题的一个例子......
Part of my problem, too, is the fact that the time that things occurred in history affect the threshold. Here's an example of part of my problem...
假设您有一根香蕉,并且您正在努力解决需要吃掉它的时间.假设它会在 3 天内过期,除非它被喷洒了某种化学物质,在这种情况下,我们会将过期时间延长 4 天,从喷洒的时间开始.然后,我们可以通过冻结它再增加 3 个月,但如果它被冻结,那么它解冻后我们只有 1 天的使用时间.
Imagine you have a banana and you're trying to work out when it needs to be eaten by. Let's say that it will expire within 3 days, unless it was sprayed with some chemical, in which case we add 4 days to the expiry, from the time the spray was applied. Then, we can add another 3 months to it by freezing it, but if it's been frozen then we only have 1 day to use it after it thaws.
所有这些规则都是由历史时间决定的.我同意我可以在几秒钟内使用 Dominik 的测试建议,但是我的历史数据呢?我应该即时创建"它吗?
All of these rules are dictated by historical timings. I agree that I could use the Dominik's suggestion of testing within a few seconds, but what of my historical data? Should I just 'create' that on the fly?
正如你可能会或可能不会说出来的,我仍在尝试掌握所有这些测试"概念;)
As you may or may not be able to tell, I'm still trying to get a hang of all of this 'testing' concept ;)
推荐答案
我最近想出了另一个解决方案,如果您使用的是 PHP 5.3 命名空间,它会非常棒.您可以在当前命名空间内实现一个新的 time() 函数并创建一个共享资源,您可以在其中设置测试中的返回值.然后对 time() 的任何不合格调用都将使用您的新函数.
I recently came up with another solution that is great if you are using PHP 5.3 namespaces. You can implement a new time() function inside your current namespace and create a shared resource where you set the return value in your tests. Then any unqualified call to time() will use your new function.
为了进一步阅读,我在 博客
For further reading I described it in detail in my blog
相关文章