Testing pdf downloads with rspec and pdfkit

I am developing a rails 3.2 application with which users can download PDF files. I really like test-based development using rspec and matts, but I don't understand this.

I have the following code inside my controller:

def show_as_pdf @client = Client.find(params[:client_id]) @invoice = @client.invoices.find(params[:id]) PDFKit.configure do |config| config.default_options = { :footer_font_size => "6", :encoding => "UTF-8", :margin_top=>"1in", :margin_right=>"1in", :margin_bottom=>"1in", :margin_left=>"1in" } end pdf = PDFKit.new(render_to_string "invoices/pdf", layout: false) invoice_stylesheet_path = File.expand_path(File.dirname(__FILE__) + "/../assets/stylesheets/pdfs/invoices.css.scss") bootstrap_path = File.expand_path(File.dirname(__FILE__) + "../../../vendor/assets/stylesheets/bootstrap.min.css") pdf.stylesheets << invoice_stylesheet_path pdf.stylesheets << bootstrap_path send_data pdf.to_pdf, filename: "#{@invoice.created_at.strftime("%Y-%m-%d")}_#{@client.name.gsub(" ", "_")}_#{@client.company.gsub(" ", "_")}_#{@invoice.number.gsub(" ", "_")}", type: "application/pdf" return true end 

This is a pretty simple code, all it does is set up my PDFKit and load the created PDF file. Now I want to check all of this, including:

  • Assigning instance variables (easy, of course, and it works)
  • Sending data, i.e. rendering pdf => And this is where I got stuck

I tried the following:

 controller.should_receive(:send_data) 

but it gives me

 Failure/Error: controller.should_receive(:send_data) (#<InvoicesController:0x007fd96fa3e580>).send_data(any args) expected: 1 time received: 0 times 

Does anyone know how to check if a pdf file is really uploaded / sent? Also, what else do you see that you need to test for good testing? For example, testing a data type, i.e. an application / pdf, would be nice.

Thanks!

+8
ruby-on-rails pdf wkhtmltopdf pdfkit rspec
source share
2 answers

Not sure why you are getting this crash, but you can check the response headers instead:

 response_headers["Content-Type"].should == "application/pdf" response_headers["Content-Disposition"].should == "attachment; filename=\"<invoice_name>.pdf\"" 

You asked for advice on better test coverage. I thought I recommend this: https://www.destroyallsoftware.com/screencasts . These screencasts have had a huge impact on my understanding of test development - highly recommended!

+15
source share

I recommend using the pdf-inspector gem to write specifications for Rails actions related to PDF.

Here's an example specification (which assumes that the Rails #report action writes Ticket model data in a generated PDF file):

 describe 'GET /report.pdf' do it 'returns downloadable PDF with the ticket' do ticket = FactoryGirl.create :ticket get report_path, format: :pdf expect(response).to be_successful analysis = PDF::Inspector::Text.analyze response.body expect(analysis.strings).to include ticket.state expect(analysis.strings).to include ticket.title end end 
+1
source share

All Articles