You Need to Start Out Testing. And Stay Testing.

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.

Matt


RailsCast 258 Token Fields bug with Rails 3.1

Ryan Bates does a great RailsCast (as usual) of the jQuery TokenInput plugin from loopj. I had been able to get this plugin working in earlier versions of Rails/jQuery, but when I tried to integrate the plugin into my Rails 3.1 app I kept getting this error:

Uncaught TypeError: Object function (a,b){return new c.fn.init(a,b)} has no method ‘type’

I am not really sure what caused this error, and I could not find any information about the error on the web. I spent a couple of hours (ok, more like 6) fiddling with different things and comparing the working version to the non-working version. I was getting ready to throw it all out and try to proceed with Chosen from Harvest, but I decided instead to try the old “just delete the offending code” trick. So, I replaced this code online 139:

if($.type(url_or_data) === “string” || $.type(url_or_data) === “function”) {

With this code:

 if(true) {//$.type(url_or_data) === “string” || $.type(url_or_data) === “function”

I am sure that this will blow something up at some point, but for now this works like a charm. If anybody has the proper way to do this, please post it in the comments (and I will update the post accordingly).

Thanks.

Matt


Follow

Get every new post delivered to your Inbox.