使用 PHPUnit 测试受保护方法的最佳实践 - Best practices to test protected methods with PHPUnit

问题:

I found the discussion on Do you test private method informative.我发现有关您是否测试私有方法的讨论内容丰富。

I have decided, that in some classes, I want to have protected methods, but test them.我已经决定,在某些类中,我想要受保护的方法,但要测试它们。 Some of these methods are static and short.其中一些方法是静态的和简短的。 Because most of the public methods make use of them, I will probably be able to safely remove the tests later.因为大多数公共方法都使用它们,所以我以后可能可以安全地删除测试。 But for starting with a TDD approach and avoid debugging, I really want to test them.但是为了从 TDD 方法开始并避免调试,我真的很想测试它们。

I thought of the following:我想到了以下几点:

  • Method Object as adviced in an answer seems to be overkill for this. 答案中建议的方法对象对此似乎有些矫枉过正。
  • Start with public methods and when code coverage is given by higher level tests, turn them protected and remove the tests.从公共方法开始,当代码覆盖率由更高级别的测试给出时,将它们保护起来并删除测试。
  • Inherit a class with a testable interface making protected methods public继承一个具有可测试接口的类,使受保护的方法公开

Which is best practice?哪个是最佳实践? Is there anything else?还有别的事吗?

It seems, that JUnit automatically changes protected methods to be public, but I did not have a deeper look at it.看起来,JUnit 会自动将受保护的方法更改为公共方法,但我没有更深入地研究它。 PHP does not allow this via reflection . PHP 不允许通过反射这样做


解决方案:

参考一: https://stackoom.com/question/12wq
参考二: Best practices to test protected methods with PHPUnit
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章