PHPUnit:如何模拟今天的日期而不将其作为参数传递? [英] PHPUnit: How to mock today's date without passing it as an argument?

查看:58
本文介绍了PHPUnit:如何模拟今天的日期而不将其作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在类上测试一个进行日期检查的方法.问题在于该方法取决于今天的日期(每天都在变化),这使其难以测试.如何模拟今天的日期,以便明天仍然可以通过考试?

I am testing a method on my class that does some date checking. The problem is that the method depends on today's date (which changes every day), which makes this difficult to test. How can I mock today's date so that my tests will still pass tomorrow?

推荐答案

我对PHP一无所知,但是在Java和C#中,我都会传递一些描述的时钟",而不是今天的日期本身,而是一个对象,您可以询问获取当前日期/时间.然后,在单元测试中,您可以传递一个对象,该对象可以提供所需的任何日期-包括硬编码到测试中的日期.

I know nothing about PHP, but in both Java and C# I would pass in a "clock" of some description - not today's date itself, but an object which you can ask for the current date/time. Then in unit tests you can pass in an object which can give any date you want - including one that's hard-coded into the tests.

在PHP中也可以使用吗?

Does that work in PHP too?

这篇关于PHPUnit:如何模拟今天的日期而不将其作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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