使用 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章