I have extended the string class as follows:
class String
def last_character
self[-1]
end
end
I have places the string.rb file in the lib as follows:
lib/core_extensions/string.rb
I have tested the setup and I can use the last_character method in a Rails console.
However, when I run an RSpec test for a class that uses the extended String class it gives me an error:
undefined method `last_character' for " ":String
Do I have to tell RSpec to load these class extension files somehow?
One way is to eagerly/explicitly load your custom extensions in the rails_helper.rb
Dir[Rails.root.join('lib/core_extensions/*.rb')].each { |f| require f }
Add that folder to eager_load_paths
and set config.eager_load = true
in config/environments/test.rb
.
Does your spec have require "rails_helper"
?
Have you tried restarting Spring?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With