单元测试魔术方法

单元测试魔术方法

本文介绍了单元测试魔术方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中魔术方法的单元测试实现时,推荐使用哪些方法来调用这些方法?

When it comes to unit-testing implementations of magic methods in PHP, what is the recommended means of invoking those methods?

我看到三个可用选项:

  • 显式/直接调用它们:

  • Invoking them explicitly/directly:

$object->__get('someValue');

间接调用它们(使用旨在触发它们的任何操作):

Invoking them indirectly (using whatever action is intended to trigger them):

$object->someValue; \\ Where __get() is implemented.

使用这两种方法调用它们.

Invoking them using both methods.

是否有任何单元测试的资深人士可以解释哪个(如果有的话)将是显而易见的选择,为什么会这样?

Are there any unit testing veterans that could explain which (if any) would be the obvious choice, and why that might be?

(这可能是在主观/辩论领域附近跳舞的,但是我希望问一下在解决这个问题时我应该考虑一些公认的原则.)

推荐答案

您应该测试可观察的行为.因此,第二个($obj->property) 必须经过测试,没问题.

You should be testing observable behavior. So, the second one ($obj->property) must be tested, no question.

至于直接直接调用getter,更多的是判断.一旦您说了__get('someProperty'),在我看来,您几乎已经陷入僵局,这是一个神奇的属性.由于我一直尝试不更改测试,因此如果出于某种原因我希望它成为普通的非魔术属性,会使事情复杂化很多.从好的方面来说,您可以调用getter并以另一种方式获取该属性,并确保它们都具有完全相同的结果.

As for actually calling the getter directly, that's more of a judgement call. Once you have said __get('someProperty'), in my opinion you've pretty much set in stone that it is a magic property. Since I try to never change the tests, that complicates things quite a bit if, for some reason, I want that to be a plain old non-magic property. On the plus side, you can call the getter and get the property the other way as well and ensure they both have the exact same result.

这篇关于单元测试魔术方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 05:12