我可以“模拟"吗?时间在PHPUnit中? [英] Can I "Mock" time in PHPUnit?

查看:61
本文介绍了我可以“模拟"吗?时间在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.

为进一步阅读,我在 查看全文

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