No stack error; how I found ObjectSpace

This is probably the strangest error I’ve yet met in Rails. I was trying out functional testing in Rails 2.3.4, with a generated scaffold. After a few changes to the model and played around with the fixtures, I banged out a ‘rake’ command. The reply I got was really strange.

Loaded suite /opt/local/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
Started
EEEEEEE
Finished in 0.125619 seconds.

1) Error:
test_should_create_user_story(UserStoriesControllerTest):
NoMethodError: undefined method `key?' for #{String:0x27c7ac0}

2) Error:
test_should_destroy_user_story(UserStoriesControllerTest):
NoMethodError: undefined method `key?' for #{String:0x27c40b4}

3) Error:
test_should_get_edit(UserStoriesControllerTest):
NoMethodError: undefined method `key?' for #{String:0x27c0798}

4) Error:
test_should_get_new(UserStoriesControllerTest):
NoMethodError: undefined method `key?' for #{String:0x27bce7c}

5) Error:
test_should_get_this_index(UserStoriesControllerTest):
NoMethodError: undefined method `key?' for #{String:0x27b9574}

6) Error:
test_should_show_user_story(UserStoriesControllerTest):
NoMethodError: undefined method `key?' for #{String:0x27b5c6c}

7) Error:
test_should_update_user_story(UserStoriesControllerTest):
NoMethodError: undefined method `key?' for #{String:0x27b2350}

Small liberties have been taken with the shell output, since WordPress doesn’t like that combination of angle brackets.

This is a very strange error to me; I’m used to being puzzled by bugs, looking through the stack trace and figuring out which function is to blame. But in this case, there was no stack trace at all!

I googled the exact error message and found someone who had a similar issue when going with rspec. Another individual, who was trying to help the first, commented ‘there should be a stack trace’.

I decided to use my good friend ‘ruby-debug’ and started going right after the class declaration of the UserStoriesController.

I wasn’t sure if I was searching at the right place. At first, I was in blankslate.rb. From there, I jumped around a while until I got to the test loader, and then entered optparse. I must have press ‘enter’ a good 500 times before I left the key depressed, speeding through a blur of jumping around in the class. When finally I left optparse, I found myself in another more obscure part of ruby, where I was looped about probably for a good five minutes. I decided to look at what this loop was doing.

I entered into irb and punched in:


ObjectSpace.each_object do |this_one|
puts this_one
end

I saw a brief flurry of code and the return of that iteration was something like 74000. Curious, I scrolled up and saw ‘everything’.

This is actually a bit scary. Every single bit of object imaginable is somewhere in this large space.

I encourage people to try this out from within Rails. I’m sure the exercise will be quite staggering.

There was simply no way I was going to make it through reading that huge pile of objects. I decided simply to type ‘continue’ in the debugger and go back to my previous, esoteric error message.

I tried google again, this time with the `key?’, exactly as per the Ruby output. I found another post on google groups where someone with that error message was asking for help. Looking at that trace, someone suggested the fix was to be made in a yml file.

And so that was it. A broken, not properly space yml file, which made Rails go nuts and swallowed my error message.

I’m wondering why Rails doesn’t include a fixtures/yml tester, to ensure that the syntax of the fixtures correct? Given that white space are counted, I don’t think I should rely on my attentiveness to recall to check on the yamls.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.