Adventures with Ruby

Prawn and controller tests

View Comments

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:

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

Written by Iain Hecker

November 18th, 2009 at 10:33 am

Posted in Uncategorized

  • Gregory Brown

    Interesting issue, but a minor correction:

    I doubt this has to do with Prawn, and instead, has to do with Prawnto, which is not an official part of Prawn anyway. Prawn itself does not touch anything at all that could conflict with Rails.

  • http://www.mhfs.com.br Marcelo Silveira

    That was incredibly handy! Thanks!

blog comments powered by Disqus
Fork me on GitHub