Monday, June 5, 2017

How to configure RSpec to load monkey patched classes

Leave a Comment

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?

2 Answers

Answers 1

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 } 

Answers 2

Does your spec have require "rails_helper"?

Have you tried restarting Spring?

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment