title: Test download of pdf with rspec and pdfkit
body: <p>I am developing a rails 3.2 application with which users can download pdfs. I enjoy test driven development a lot using rspec and shoulda matchers, but I'm at a loss with this one.</p>
  
  <p>I have the following code inside my controller:</p>
  
  <pre><code>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 =&gt; "6",
        :encoding =&gt; "UTF-8",
        :margin_top=&gt;"1in",
        :margin_right=&gt;"1in",
        :margin_bottom=&gt;"1in",
        :margin_left=&gt;"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 &lt;&lt; invoice_stylesheet_path
    pdf.stylesheets &lt;&lt; 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
  </code></pre>
  
  <p>This is fairly simple code, all it does is configure my PDFKit and download the generated pdf. Now I want to test the whole thing, including:</p>
  
  <ul>
  <li>Assignment of instance variables (easy, of course, and that works)</li>
  <li>The sending of data, i.e. the rendering of the pdf => And this is where I'm stuck</li>
  </ul>
  
  <p>I have tried the following:</p>
  
  <pre><code>controller.should_receive(:send_data)
  </code></pre>
  
  <p>but that gives me</p>
  
  <pre><code>Failure/Error: controller.should_receive(:send_data)
     (#&lt;InvoicesController:0x007fd96fa3e580&gt;).send_data(any args)
         expected: 1 time
         received: 0 times
  </code></pre>
  
  <p>Does anyone know of a way to test that the pdf is actually downloaded/sent? Also, what more things do you see that should be tested for good test coverage? E.g., testing for the data type, i.e. application/pdf, would be nice.</p>
  
  <p>Thanks!</p>