【问题标题】:Test download of pdf with rspec and pdfkit使用 rspec 和 pdfkit 测试下载 pdf
【发布时间】:2013-03-07 17:05:56
【问题描述】:

我正在开发一个 Rails 3.2 应用程序,用户可以使用它下载 pdf。我非常喜欢使用 rspec 和 shoulda 匹配器进行测试驱动开发,但我对这个感到茫然。

我的控制器中有以下代码:

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

这是相当简单的代码,它所做的只是配置我的 PDFKit 并下载生成的 pdf。现在我想测试整个事情,包括:

  • 分配实例变量(当然很简单,而且很有效)
  • 数据的发送,即 pdf 的渲染 => 这就是我卡住的地方

我尝试了以下方法:

controller.should_receive(:send_data)

但这给了我

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

有谁知道测试 pdf 是否实际下载/发送的方法?此外,您还看到哪些内容需要测试以获得良好的测试覆盖率?例如,测试数据类型,即 application/pdf,会很好。

谢谢!

【问题讨论】:

    标签: ruby-on-rails pdf rspec wkhtmltopdf pdfkit


    【解决方案1】:

    不确定为什么会失败,但您可以改为测试响应标头:

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

    您就更好的测试覆盖率寻求建议。我想我会推荐这个:https://www.destroyallsoftware.com/screencasts。这些截屏视频对我对测试驱动开发的理解产生了巨大影响——强烈推荐!

    【讨论】:

    • 谢谢,但不幸的是,这给了我undefined method 'response_headers' - 我做错了什么?
    • 哈哈,我忘了get :show_as_pdf :) 这解决了所有问题。非常感谢!
    • 我想测试一下下载的内容,但是用selenium,好像不行,有人知道吗?
    • 使用 response.headers["Content-Type"] 对我有用
    • 它适用于我的 rails 4 / rspec 3.5.0 但不适用于 rails 5。有什么想法吗?
    【解决方案2】:

    我建议使用 pdf-inspector gem 来编写与 PDF 相关的 Rails 操作的规范。

    这是一个示例规范(假设 Rails #report 操作在生成的 PDF 中写入有关 Ticket 模型的数据):

    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
    

    【讨论】:

    • 注意:这个规范应该是一个请求规范,而不是控制器规范,后者使用空字符串存根render
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多