From: Bernhard Brodowsky Date: 2010-04-07T19:57:16+09:00 Subject: Re: Visibility in tests Ryan Davis wrote: > On Apr 6, 2010, at 17:26 , Bernhard Brodowsky wrote: > >> I don't know how to apply unit tests to private methods. Usually, I only >> use very few private methods and for my small private purposes, it was >> okay to just set those few to public, too, but how do you do that in a >> good way, if you really do have good reasons for making the methods >> private? > > easy! make them public! > > Either don't make them private in the first place (my preference) or > make them public in your tests (second choice... usually when I'm > working with inherited code): > > class X > public :previously_private_method > end > > class TestX < MiniTest::Unit::TestCase > def test_previously_private_method > ... > end > end Okay, thanks, a lot, nice and easy idea, I should have seen that. :) Brian Candler wrote: > Ryan Davis wrote: >> On Apr 6, 2010, at 17:26 , Bernhard Brodowsky wrote: >> >>> I don't know how to apply unit tests to private methods. Usually, I only >>> use very few private methods and for my small private purposes, it was >>> okay to just set those few to public, too, but how do you do that in a >>> good way, if you really do have good reasons for making the methods >>> private? >> >> easy! make them public! > > Or use one of: > > obj.send(:private_method_name, args...) > > obj.instance_eval { private_method_name args... } > > Note that the latter also switches the object context so that '@foo' > inside the block means the @foo instance variable inside obj. That can > be useful too. Thanks a lot, I think the possibility to access instance variables from outside will also be very useful for other purposes. Okay, now I will have no problems anymore to write tests for private methods.^^ -- Posted via http://www.ruby-forum.com/.