Isnor Creative
Isnor Creative Blog
Ruby, Ruby on Rails, Ember, Elm, Phoenix, Elixir, React, Vue

May 9, 2014

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.

Gordon B. Isnor

Gordon B. Isnor writes about Ruby on Rails, Ember.js, Elm, Elixir, Phoenix, React, Vue and the web.
If you enjoyed this article, you may be interested in the occasional newsletter.

I am now available for project work. I have availability to build greenfield sites and applications, to maintain and update/upgrade existing applications, team augmentation. I offer website/web application assessment packages that can help with SEO/security/performance/accessibility and best practices. Let’s talk

comments powered by Disqus