Stubbing out WickedPdf::PdfHelper#make_pdf
as in the other answer prevents testing what's being rendered with e.g. expect(response).to render_template("show")
.
To find which methods lead to writing to files I temporarily added expect(File).not_to receive(:open)
before the get ... format: :pdf
and found:
Failure/Error: render pdf: "...", template: "show"
(File (class)).open("/tmp/wicked_pdf20200304-24076-r7r1eh.html", 194, {:perm=>384})
expected: 0 times with any arguments
received: 1 time with arguments: ("/tmp/wicked_pdf20200304-24076-r7r1eh.html", 194, {:perm=>384})
# /usr/local/bundle/gems/wicked_pdf-1.4.0/lib/wicked_pdf/tempfile.rb:10:in `initialize'
# /usr/local/bundle/gems/wicked_pdf-1.4.0/lib/wicked_pdf.rb:58:in `new'
# /usr/local/bundle/gems/wicked_pdf-1.4.0/lib/wicked_pdf.rb:58:in `pdf_from_string'
# /usr/local/bundle/gems/wicked_pdf-1.4.0/lib/wicked_pdf/pdf_helper.rb:91:in `make_pdf'
# /usr/local/bundle/gems/wicked_pdf-1.4.0/lib/wicked_pdf/pdf_helper.rb:113:in `make_and_send_pdf'
# /usr/local/bundle/gems/wicked_pdf-1.4.0/lib/wicked_pdf/pdf_helper.rb:40:in `render_with_wicked_pdf'
# /usr/local/bundle/gems/wicked_pdf-1.4.0/lib/wicked_pdf/pdf_helper.rb:30:in `render'
# ./app/controllers/reports_controller.rb:21:in `block (2 levels) in show'
# ./app/controllers/reports_controller.rb:11:in `show'
I then experimented with stubbing at different places in this backtrace and found that the following makes the test as fast as the usual "html" controller tests while still allowing to test for render_template("show")
:
it "returns http success" do
allow_any_instance_of(WickedPdf::PdfHelper).to receive(:make_and_send_pdf)
get :show, params: { id: 1, format: :pdf }
expect(response).to have_http_status(:success)
end
it "renders the show template" do
allow_any_instance_of(WickedPdf::PdfHelper).to receive(:make_and_send_pdf)
get :show, params: { id: 1, format: :pdf }
expect(response).to render_template("show")
end
this also makes returning tiny pdf unnecessary.