0
votes
  • ruby 2.3.1
  • rails (3.2.22.4)
  • rspec-rails (2.14.2)
  • pry-rails (0.3.2)
  • pry-byebug (3.6.0)
  • pry (0.10.4)

I am using a bit old version of Ruby and Rails.

I am running RSpec tests in the Ubuntu Terminal:

rspec ./spec/features/some_spec.rb

and I am using pry breakpoints (binding.pry - inside my RSpec tests).

I am missing the reload! 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 reload! method defined and working in Rails console - but not in the RSpec debug breakpoint.

How to set up reload! in RSpec?

I tried the solutions on pry gem how to reload? , but neither worked for me.

This one appears to be the closest to the real solution: https://stackoverflow.com/a/50966785/6594668 , it does actually define reload! - but this reload! does nothing.

A quick sample:

class User < ActiveRecord::Base
  def foo1
    p 123
  end
end

When I run Rails console, change the method name from foo1 to foo2, call reload! - then these calls work OK:

User.first.foo1
=> 123
#change the method name (by manually editing the file)
User.first.foo2
=> 123

But - when I do the same thing in RSpec debug breakpoint - it does not actually reload the code - even if I manually define reload! with either of 2 ways:

1. https://stackoverflow.com/a/50966785/6594668

def reload!(print=true)
  puts "Reloading..." if print
  ActionDispatch::Reloader.cleanup!
  ActionDispatch::Reloader.prepare!
  true
end

2. https://github.com/rweng/pry-rails/issues/99#issuecomment-364272642

Object.send(:include, Rails::ConsoleMethods)

Looks like something I still am missing (for reload! to work in RSpec).

1

1 Answers

1
votes

OK, I just found it.

The only thing I needed to change was this:

config/environments/test.rb

MyApp::Application.configure do
  # config.cache_classes = true
  config.cache_classes = false
end

Now in RSpec breakpoints I can successfully reload the changed code by calling reload! - just exactly the same way as in the Rails console.