What's the best way to unit test protected & private methods in Ruby?

后端 未结 16 632
被撕碎了的回忆
被撕碎了的回忆 2020-12-12 10:47

What\'s the best way to unit test protected and private methods in Ruby, using the standard Ruby Test::Unit framework?

I\'m sure somebody will pipe up a

16条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 11:33

    You can bypass encapsulation with the send method:

    myobject.send(:method_name, args)
    

    This is a 'feature' of Ruby. :)

    There was internal debate during Ruby 1.9 development which considered having send respect privacy and send! ignore it, but in the end nothing changed in Ruby 1.9. Ignore the comments below discussing send! and breaking things.

提交回复
热议问题