Skip to content

Instantly share code, notes, and snippets.

@bdezonia
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdezonia/658f05e00cee673fd944 to your computer and use it in GitHub Desktop.
Save bdezonia/658f05e00cee673fd944 to your computer and use it in GitHub Desktop.
A monkey patch that fixes missing_method stack errors in rspec tests
Hopefully this will be of some use to people supporting old rails versions.
I tried porting a rails 2.3 application that had been on ruby 1.8.7 to ruby 1.9.3.
After doing so I could not get my rspec 1.3 tests to run successfully (in my
controllers). I always got stack level too deep errors dealing with a NoMethodError.
I could see that an Array#flatten! call was the culprit.
I understood from this post
(http://yehudakatz.com/2010/01/02/the-craziest-fing-bug-ive-ever-seen/) that somewhere
in the Rails 1.9 development the way method_missing was handled was changed somewhat.
And that any code that overrode method_missing might conflict. I was using the
remarkable and remarkable_rails gems and they indeed were overriding method_missing.
To finally fix the problem I did a surgical monkey patch which I have listed below.
put this code in a file called test_patch.rb in my config/initializers directory.
Once in place my tests started passing.
if (RAILS_ENV == 'test')
require 'spec'
Spec::Example::ExampleGroupHierarchy.module_eval do
def flatten!
contents = flatten_contents self
size.times do | i |
delete_at(0)
end
contents.each do | thing |
self << thing
end
end
def flatten_contents nested_array
result = []
nested_array.each do | item |
if item.kind_of?(Array)
more_stuff = flatten_contents item
more_stuff.each do | stuff |
result << stuff
end
else
result << item
end
end
result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment