使用标准的 RubyTest::Unit
框架,在 Ruby 中进行单元测试受保护和私有方法的最佳方法是什么?
我确信有人会主动提出并武断地断言“你应该只对公共方法进行单元测试; 如果它需要单元测试,它就不应该是受保护的或私有的方法”,但我对这个问题并不感兴趣。我有几个 是保护或私有的方法,这些私有/保护的方法比较复杂,类中的公共方法依赖于这些保护/私有的方法正常工作,因此我需要一种方法来测试保护/私有的方法。
还有一件事... ... 我通常将给定类的所有方法放在一个文件中,将该类的单元测试放在另一个文件中。理想情况下,我希望所有的魔法都实现这个“受保护和私有方法的单元测试”功能到单元测试文件中,而不是主源文件中,以便使主源文件尽可能简单和直接。