Thursday, 4 November 2010

RSpec/Textmate puts for better debugging printouts

I loved this awesome tip by Rick DeNatale on how to get Textmate to properly print out "puts" when running rspec tests. But I had a problem. When I'd run the code outside of rspec, the use of rputs would cause an exception because the method was undefined. So I've changed the code a little bit so it works in all instances. Add the following code as the very last lines of spec/spec_helper.rb:
module Kernel
  if ENV.keys.find {|env_var| env_var.match(/^TM_/)}
    def puts_with_html(*args)
      puts_without_html( *["<pre>", args.collect {|a| CGI.escapeHTML(a.to_s)}, "</pre>"])
    end
    alias_method_chain :puts, :html
  end
end

No comments: