Archive for the 'Rails' Category

Fix nil.rewrite errors in your Helper Tests

Sunday, December 14th, 2008

In testing my helpers, I discovered a nil.rewrite exception due to ActionController::Base.initialize_current_url not being called. This occurs when using url_for with a hash as arguments.

ActionView::TestCase does not initialize current url so you won’t be able to use *_url and *_path helpers generated from your routes. Put the following lines at the end of your test_helper.rb file to resolve the issue.

class ActionView::TestCase < ActiveSupport::TestCase
  class TestController < ActionController::Base
    attr_accessor :request, :response, :params

    def initialize
      @request = ActionController::TestRequest.new
      @response = ActionController::TestResponse.new
      
      #TestCase doesn't have context of a current url so cheat a bit
      @params = {}
      send(:initialize_current_url)
    end
  end
end

I've submitted a patch to core regarding this. Please +1 for the good of helper_tests!

Get Your Exceptions and Errors Organized with Hoptoad

Thursday, August 14th, 2008

Goodbye exception_notifier and a cluttered inbox! Kudos to Thoughtbot for rolling out hoptoad: the app error app. It’s a web service that your app can talk to in order to report 500 (server) errors.

It was a snap to configure and has a simple plugin you can download once you register. You can add hooks to Lighthouse or other bug tracking software. Not sure how to do this yet, but it’s awesome to see a great execution on an app that solves a problem.

RSpec Report in Text Format

Saturday, August 2nd, 2008

I recently wanted to share RSpec examples with a business user. For some reason, it was kind of hard to find information on how to get a nice listing of all the examples present in your system. I believe Text and HTML formats are available, but I was unsure about how to pass spec command line options to the rake task.

This is how to get it done. On the command line from your Rails project’s root:

  rake spec SPEC_OPTS="--format s -c"

This is a great way to validate a data model in plain English.

Not using RSpec? I highly recommend using it or a comparable framework to use business language as a definition for technical tests. Remember, if you follow Agile methodologies, you’ll want to test before you implement any features.

Are you interested in learning more about Behavior Driven Development (BDD) and RSpec? Check out Peepcode’s screencast.

Rails Validation fieldWithErrors annoyance

Thursday, July 3rd, 2008

So I’m thoroughly annoyed that a fieldWithErrors div wraps around a field that fails validation. I added a file called field_with_error_fix.rb in config/initializers to solve this pesky problem. It has the following code:

ActionView::Base.field_error_proc = Proc.new { |html_tag, instance|
"<span class=\"fieldWithErrors\">#{html_tag}</span>" }

Goodbye inappropriate divs!

What’s New In Rails 2.1 – A Great Reference

Monday, June 23rd, 2008

I found this today – kudos to Carlos Brando for putting together an awesome reference! It’s a summary of all the new features rolled into Ruby on Rails 2.1.

Ruby on Rails 2.1 – What’s New