Testing Prawn PDF generation using Rspec
I was recently pleased to discover how easy it was to test a PDF generation service built on top of Prawn in a Rails app.
First I added the pdf-inspector gem to Gemfile:
gem 'pdf-inspector', require: "pdf/inspector", group: :test
Next, a spec:
require 'spec_helper'
describe 'book_pdf' do
before do
@book = FactoryGirl.build_stubbed(:book).decorate
view_context = ActionController::Base.new.view_context
pdf = BookPdf.new(@book, view_context)
rendered_pdf = pdf.render
@text_analysis = PDF::Inspector::Text.analyze(rendered_pdf)
end
specify { @text_analysis.strings.should include(@book.title) }
end
https://github.com/prawnpdf/pdf-inspector
That’s it! Great testing tool.
I am available for Ruby on Rails consulting work – get in touch to learn more.