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

后端 未结 16 601
被撕碎了的回忆
被撕碎了的回忆 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:12

    You can "reopen" the class and provide a new method that delegates to the private one:

    class Foo
      private
      def bar; puts "Oi! how did you reach me??"; end
    end
    # and then
    class Foo
      def ah_hah; bar; end
    end
    # then
    Foo.new.ah_hah
    

提交回复
热议问题