title: Rails RSpec Pry "reload!"
body: <ul>
  <li>ruby 2.3.1</li>
  <li>rails (3.2.22.4)</li>
  <li>rspec-rails (2.14.2)</li>
  <li>pry-rails (0.3.2)</li>
  <li>pry-byebug (3.6.0)</li>
  <li>pry (0.10.4)</li>
  </ul>
  
  <p>I am using a bit old version of Ruby and Rails.</p>
  
  <p>I am running RSpec tests in the Ubuntu Terminal:</p>
  
  <pre><code>rspec ./spec/features/some_spec.rb
  </code></pre>
  
  <p>and I am using <code>pry</code> breakpoints (<code>binding.pry</code> - inside my RSpec tests).</p>
  
  <p>I am missing the <code>reload!</code> method while debugging the code in these breakpoints (under RSpec only). (I mean dynamically reload the changed model code - like adding new methods on the fly etc.) This method is even not defined there. I do have the <code>reload!</code> method defined and working in Rails console - but not in the RSpec debug breakpoint.</p>
  
  <p>How to set up <code>reload!</code> in RSpec?</p>
  
  <p>I tried the solutions on <a href="https://stackoverflow.com/questions/7347327/pry-gem-how-to-reload">pry gem how to reload?</a> , but neither worked for me.</p>
  
  <p>This one appears to be the closest to the real solution: <a href="https://stackoverflow.com/a/50966785/6594668">https://stackoverflow.com/a/50966785/6594668</a> , it does actually define <code>reload!</code> - but this <code>reload!</code> does nothing.</p>
  
  <p>A quick sample:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    def foo1
      p 123
    end
  end
  </code></pre>
  
  <p>When I run Rails console, change the method name from <code>foo1</code> to <code>foo2</code>, call <code>reload!</code> - then these calls work OK:</p>
  
  <pre><code>User.first.foo1
  =&gt; 123
  #change the method name (by manually editing the file)
  User.first.foo2
  =&gt; 123
  </code></pre>
  
  <p>But - when I do the same thing in RSpec debug breakpoint - it does not actually reload the code - even if I manually define <code>reload!</code> with either of 2 ways:</p>
  
  <p>1.
  <a href="https://stackoverflow.com/a/50966785/6594668">https://stackoverflow.com/a/50966785/6594668</a></p>
  
  <pre><code>def reload!(print=true)
    puts "Reloading..." if print
    ActionDispatch::Reloader.cleanup!
    ActionDispatch::Reloader.prepare!
    true
  end
  </code></pre>
  
  <p>2.
  <a href="https://github.com/rweng/pry-rails/issues/99#issuecomment-364272642" rel="nofollow noreferrer">https://github.com/rweng/pry-rails/issues/99#issuecomment-364272642</a></p>
  
  <pre><code>Object.send(:include, Rails::ConsoleMethods)
  </code></pre>
  
  <p>Looks like something I still am missing (for <code>reload!</code> to work in RSpec).</p>