You Need to Start Out Testing. And Stay Testing.Posted: February 18, 2012
I spent a couple of hours (or more) working on an annoying problem. I had been slacking on my tests for a while, and built a bunch of code without any testing. It was Friday night, so I decided it was time to pay down a bit of my technical debt, and now it is Saturday morning. Alas, test early, test always, and I would have caught this mistake way earlier.
Now on to the details. I have a test setup modeled after Ryan Bates, which involves Capybara, Factory Girl, and Rspec. The error I was getting was across a number of my tests:
Failure/Error: click_button 'Log in' ActionView::Template::Error: comparison of Fixnum with nil failed # ./app/views/contacts/index.html.erb:30:in `<' ... # ./spec/requests/authentications_spec.rb:24:in `block (3 levels) in '
Literally, there is nothing across all of the Internets that gave me any clues, and I was thinking that it had something to do with Capybara. I was able to interactively log in, even when I fired up Rails in an interactive test environment (rails server -e test).
It turns out that I had not set some variables in Factory Girl, so their values were nil, and when I tried to call a comparison against the nil I got the “comparison of Fixnum with nil failed.” Makes sense, and way easier than some of the stuff that I had considered. But, a couple of hours forever lost because I did not keep up with my testing. Lesson learned. Again:)
If you have better ways to test or debug, I would love to hear them.