Prawn and controller tests

prawn_logoThere is a real annoying gotcha in using controller tests to test an action that renders a pdf with Prawn. You’ll get a NoMethodException on “nil.downcase”. The troubling part is that it totally puts you off by providing the wrong lines and backtrace.

This has been mentioned somewhere on some mailinglists, but to make it a bit more findable, I’d thought I’d post it here.

The solutionworkaround is to set the server protocol, like this:

    it "should show the pdf" do
      request.env["SERVER_PROTOCOL"] = "http"
      get :show, :id => "report", "format" => "pdf"
      response.should render_template(:show)
    end

Configuring Autotest

I have a big test suite in the current Rails application I’m working on. I have 2340 examples in RSpec, taking over 2 minutes to run. This is an absolute pain to run. Luckily there is autotest (or autospec if you’re running RSpec, like I am), which tests only the changed files. I’ve grown to be totally dependent on this behavior, and I can’t imagine programming without it anymore.

I also do TDD, which means that I write a failing test first, and then program until it passes. But Autotest’s flow is that, once you’ve fixed a failing test or spec, it reruns the entire the suite to see if you’re solution doesn’t have any side effects. Normally this is fine, but with this kind of test suite, I cannot afford to wait for it to complete.

So, after going through Autotest’s code, I’ve decided to stub out this behavior. You can still trigger a complete rerun of the entire suite by pressing Ctrl+C, but it doesn’t do that every time you go green. It’s a bit of a monkey patch, but it works just right.

The autotest-growl gem clears the terminal. I don’t like that, because I like to see a bit of history. That’s why I changed that behavior too.

Here’s my ~/.autotest file:

# Use file system hooks on OS X
require 'autotest/fsevent'

# Don't run entire test suite when going from red to green
class Autotest
  def tainted
    false
  end
end

# Use Growl support
require 'autotest/growl'

# Don't clear the terminal, when using Growl
module Autotest::Growl
  @@clear_terminal = false
end

While browsing through the code of Autotest I also found that it also looks for a .autotest file in the current working directory. So if you want to apply these changes to one project only, you can define this file locally for the project. I didn’t know that!

Red -> Green -> Eat… eh… Refactor” title=”red_green_apples” width=”500″ height=”206″ class=”size-full wp-image-523″ /><p class=Red -> Green -> Eat... eh... Refactor

Plugin release: Root table

Yes, I’ve written another plugin for Rails. It’s about so called root tables. It seem to be making them often. I want a list with options to choose from and some easy way to manage that list, which is a tedious task. That’s why I made a plugin to do this for me.

What I have so far is:

  • Automatic validations and relations
  • Completely configurable, with sensible defaults
  • A management interface
  • Works with acts_as_list and supports drag and drop sorting
  • I18n support

Read more

Silencing Passenger

When using Rails 2.3 and Passenger, you can do yourself a favor by adding this line to config/silencers/backtrace_silencer.rb

Rails.backtrace_cleaner.add_silencer { |line| line =~ /^\s*passenger/ }

sssh
Saves you scrolling through the endless backtraces passenger gives you for free :)

PS. A colleague tweeted this lovely backtrace of a spring with grails error. I say: backtrace cleaner FTW!

Filtering with named scopes (encore)

In my previous post, I talked about making filters using named scopes. To summorize:

I like the method of using a named_scope and delegating to specified filters. This way, you can structure your filters properly and get clean URLs. Also, you can chain other named scopes to the filter.

If you find yourself making an administrative web application, with many tables and filters, here’s an example to make it a little more DRY.
Read more

Return top

About me

I am a Ruby programmer from Rotterdam, Netherlands. I started working with Ruby on Rails about 3 years ago and I've fallen in love with the language ever since.

When learning Ruby I noticed that the most useful information comes from blogs of other Ruby developers. I try to contribute to that.

I also am into writing plugins and gems. I hope you like my contributions and leave a comment if you do.