Testing prawn nil.downcase

46 views
Skip to first unread message

David Siñuela Pastor (Siu)

unread,
May 28, 2009, 7:32:56 AM5/28/09
to Prawn
Hello,

I'm having problems testing prawn/prawnto inside my rails application.
I added a simple test to check if the controller is generating a pdf
file, but I'm having a weird error, it is complaining about a
nil.downcase exception, even with an empty template. BUT it is working
in the web browser, so the problem must be in the test code, can you
help me?

Versions:

prawn (0.4.1)
prawn-format (0.1.1)
rails (2.3.2)
prawnto plugin latest version from github

I made a new rails project with the following code:

1) Error:
test_should_show_book_pdf(BooksControllerTest):
ActionView::TemplateError: You have a nil object when you didn't
expect it!
The error occurred while evaluating nil.downcase
On line #1 of app/views/books/show.pdf.prawn

1: pdf.text "Book"

app/views/books/show.pdf.prawn:1:in
`_run_prawn_app47views47books47show46pdf46prawn'
app/controllers/books_controller.rb:21:in `show'
app/controllers/books_controller.rb:18:in `show'
/test/functional/books_controller_test.rb:29:in
`test_should_show_book_pdf'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:34:in `run'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `each'
/usr/lib/ruby/1.8/test/unit/testsuite.rb:33:in `run'
/usr/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:46:in
`run_suite'
/usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:67:in
`start_mediator'
/usr/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:41:in
`start'
/usr/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:29:in
`run'
/usr/lib/ruby/1.8/test/unit/autorunner.rb:216:in `run'
/usr/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run'
/usr/lib/ruby/1.8/test/unit.rb:278
rake (0.8.7) lib/rake/rake_test_loader.rb:5

In environment.rb I have:
config.gem "prawn"
config.gem "prawn-format", :lib => 'prawn/format'

application/views/books/show.pdf.prawn:
pdf.text "Book"

application/controllers/books_controller.rb:
# GET /books/1
# GET /books/1.xml
def show
@book = Book.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @book }
format.pdf { render :layout => false }
end
end

test/functionals/books_controller_test.rb:
test "should show book pdf" do
get :show, {:id => books(:one).to_param, :format => 'pdf'}
assert_response :success
end

I tried with:
test "should show book pdf" do
get :show, {:id => books(:one).to_param, :format => :pdf}
assert_response :success
end
And it was replying with a 406 response code.

I don't know what else I could try. Can you give me some advice?

SteveQ

unread,
Jun 5, 2009, 8:02:48 AM6/5/09
to Prawn
Just came across the same problem. You can get around it by modding/
patching 'ssl_required?' in 'compile_support.rb' of prawnto. The
server protocol variable needs a nil check.

def ssl_request?
@controller.request.env['SERVER_PROTOCOL'].try(:downcase) ==
"https"
end

Alternatively you could try stubbing out ssl_request? in your test

Steve

On May 28, 12:32 pm, David Siñuela Pastor (Siu) <siu.

David Siñuela Pastor (Siu)

unread,
Jun 5, 2009, 12:38:08 PM6/5/09
to Prawn
Thank you! The tests are passing now :)

I didn't know where the problem came from.
Reply all
Reply to author
Forward
0 new messages