blog.absurd:li - press play on tape
September 29th 2009
Tagged rails, ruby, rspec, tricks

A useful addition to your spec_helper.rb

Here’s a little something that is really useful to put at the end of your spec/spec_helper.rb:


  # Run away with this. However, if it causes a nightly bug hunt or two, don't
  # blame me. Please. 
  class EscapedOut
    def initialize(old_io)
      @old_io = old_io
    end
    def write(str)
      @old_io.write str.gsub(/</, '&lt;')
    end
  end
  $stdout = EscapedOut.new($stdout)  

Afterwards, to quickly debug your specs in TextMate, you just enter


  it "some specification" do
    p some_object  # temporary debug code
    
    # some test here that fails for unknown reasons
  end

and it will just work, outputting the string you’d expect and not a very short line starting with ‘#’ and then nothing. The trick is that the string that is originally output (something like ‘#<Foo:0x128a964>’) is converted into #&amp;lt;Foo:0x128a964> which will output just fine.

You’re welcome.