开发者

Yii framework unit Testing. How to mock time() and other standart functions?

开发者 https://www.devze.com 2023-03-29 02:45 出处:网络
I\'m writing unit test for my models. Most of them use many standart functions, like time(), date() and so on. What is best approach to mock these functions in Yii testing style?

I'm writing unit test for my models. Most of them use many standart functions, like time(), date() and so on. What is best approach to mock these functions in Yii testing style?

I found PHPUnit extension Mockfunction, but i think, that there is more elegant way to mock functions in Yii.

Help pls. 开发者_StackOverflow社区Some examples will be very pleasant.


I know that this question is old, but it's interesting nevertheless.

In short, if you have a library function like time() in your code, then obviously you cannot mock it as is. You should have some sort of completely dumb abstraction around this call like TimeSource->now() and your SUT should use this abscraction instead of direct call to library function. This way in your unit tests you can substitute real TimeSource with the fake created in any way you will find suitable, via Mockery, via PHPUnit mocks, writing fake class by hand, you name it. This fake should return controllable results for function in question, of course.

0

精彩评论

暂无评论...
验证码 换一张
取 消