Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>instance_eval()</code> might help:</p> <pre><code>--------------------------------------------------- Object#instance_eval obj.instance_eval(string [, filename [, lineno]] ) =&gt; obj obj.instance_eval {| | block } =&gt; obj ------------------------------------------------------------------------ Evaluates a string containing Ruby source code, or the given block, within the context of the receiver (obj). In order to set the context, the variable self is set to obj while the code is executing, giving the code access to obj's instance variables. In the version of instance_eval that takes a String, the optional second and third parameters supply a filename and starting line number that are used when reporting compilation errors. class Klass def initialize @secret = 99 end end k = Klass.new k.instance_eval { @secret } #=&gt; 99 </code></pre> <p>You can use it to access private methods and instance variables directly.</p> <p>You could also consider using <code>send()</code>, which will also give you access to private and protected methods (like James Baker suggested)</p> <p>Alternatively, you could modify the metaclass of your test object to make the private/protected methods public just for that object.</p> <pre><code> test_obj.a_private_method(...) #=&gt; raises NoMethodError test_obj.a_protected_method(...) #=&gt; raises NoMethodError class &lt;&lt; test_obj public :a_private_method, :a_protected_method end test_obj.a_private_method(...) # executes test_obj.a_protected_method(...) # executes other_test_obj = test.obj.class.new other_test_obj.a_private_method(...) #=&gt; raises NoMethodError other_test_obj.a_protected_method(...) #=&gt; raises NoMethodError </code></pre> <p>This will let you call these methods without affecting other objects of that class. You could reopen the class within your test directory and make them public for all the instances within your test code, but that might affect your test of the public interface.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload