How to test Prawn + Rspec + Capybara on Ruby on Rails?
Autor: Jackson Pires | Publicado: 2014-03-26T05:44:00-07:00



Eu estava procurando como testar&nbsp;Prawn + Rspec + Capybara&nbsp;no Ruby on Rails e encontrei alguns exemplos na Internet, mas, na maioria dos casos, os testes envolvendo o &nbsp;Prawn + Rspec&nbsp;estavam no "formado ruby", sem os famosos arquivos de view *.pdf.prawn. Decidi então testar de um modo diferente pois eu queria testar do mesmo jeito que o usuário usa a aplicação, clicando em um link/botão e vendo um PDF no navegador.

Pois bem, esse teste pode ser feito com o&nbsp;Rspec +&nbsp;Capybara&nbsp;+ PDF-Inspector&nbsp;que inspeciona o PDF, validando o conteúdo gerado.

Para fazer o teste siga os passos:

&gt;&gt; Você pode ver o exemplo completo no Github &lt;&lt;

Criando o PDF e visualizando no seu browser.

1) Coloque essas gems na sua&nbsp;Gemfile:

gem 'prawn'
gem 'prawnto_2', :require =&gt; 'prawnto'
gem 'rspec-rails', '~&gt; 2.0'
gem 'capybara'
gem 'pdf-inspector'


2) Crie um link com o famoso helper&nbsp;"link_to" garantindo que o&nbsp;&nbsp;:format =&gt; :pdf&nbsp; foi usado.

link_to "click here to generates the PDF", my_link_path(:format =&gt; :pdf)


3) Garanta que o controller/action usados no link renderiza o formato PDF.

respond_to {|format| format.pdf}


4) Crie uma view com a extensão&nbsp;*.pdf.prawn&nbsp;e coloque os comandos prawn dentro dela para gerar seu PDF.



5) Neste momento, o link pode ser clicado e vocẽ verá o PDF no seu navegador.





Criando os testes com Rspec&nbsp;+ Capybara&nbsp;+ PDF-Inspector



1) Garanta que o Capybara esteja integrado com o rspec-rails, para isso, no arquivo&nbsp;spec_helper.rb&nbsp;coloque as duas seguintes linhas:


require "capybara/rspec"
require " capybara/rails"


3) Em alguns casos é necessário incluir a Capybara DSL no Rspec config do do arquivo spec_helper.rb&nbsp;também:



Rspec.configure {|config| config.include Capybara::DSL}



4) Crie seu teste:


require "spec_helper"

describe "Testing Prawn" do
&nbsp; &nbsp; context "PDF" do
&nbsp; &nbsp; &nbsp; &nbsp; it "Shows a PDF by clicking in the link" do
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visit "/my_link.pdf"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ext_analysis = PDF::Inspector::Text.analyse(page.body)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect(ext_analysis.strings).to be_include("some text that you put in your pdf")
&nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; end
end&nbsp;



Tudo pronto!



Este é o teste que tenho usado para garantir os passos que o usuário fará e também garantir que o conteúdo gerado pelo prawn é o esperado.


Talvez essas explicação não tenha sido o bastante, então, lembre-se que você pode ver o exemplo completo no Github!




==============
In English...

I was searching&nbsp;how to test Prawn + Rspec + Capybara on Ruby on Rails then I found some examples in a Internet but, in the most of cases, the test made by Prawn + Rspec&nbsp;were in "ruby format", without the famous *.pdf.prawn view files. Then I decided to do a test of a different way, because I have want to test in the same way how the user to do when use the application, clicking in a button/link and see &nbsp;a PDF in a browser.

This test can be done by using Rspec +&nbsp;Capybara&nbsp;+ PDF-Inspector that inspect the pdf, validating the generated content.

To do this test follow the steps

&gt;&gt; You can see the entire example on Github &lt;&lt;

Creating and using the PDF with views on your browser.

1) Put these gems on you Gemfile:

gem 'prawn'
gem 'prawnto_2', :require =&gt; 'prawnto'
gem 'rspec-rails', '~&gt; 2.0'
gem 'capybara'
gem 'pdf-inspector'


2) Creates a link with the famous "link_to" helper on the view that goes a link to your PDF and ensure the :format =&gt; :pdf are setted.

link_to "click here to generates the PDF", my_link_path(:format =&gt; :pdf)


3) Ensure that the controller/action that you put in the link render the PDF format.

respond_to {|format| format.pdf}


4) Creates a view with the *.pdf.prawn extension and put the prawn commands inside to generates your PDF.



5) In this time, the link can be clicked and you will see the PDF in your browser.





Creating the tests with Rspec&nbsp;+ Capybara&nbsp;+ PDF-Inspector



1) Ensure the Capybara is integrated with rspec-rails, for this, in the spec_helper.rb file put the two follow lines:


require "capybara/rspec"
require " capybara/rails"


3) In some cases is needed to include the Capybara DSL on config Rspec, in spec_helper.rb too:



Rspec.configure {|config| config.include Capybara::DSL}



4) Creates your test:




require "spec_helper"

describe "Testing Prawn" do
&nbsp; &nbsp; context "PDF" do
&nbsp; &nbsp; &nbsp; &nbsp; it "Shows a PDF by clicking in the link" do
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visit "/my_link.pdf"
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ext_analysis = PDF::Inspector::Text.analyse(page.body)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; expect(ext_analysis.strings).to be_include("some text that you put in your pdf")
&nbsp; &nbsp; &nbsp; &nbsp; end
&nbsp; &nbsp; end
end&nbsp;



All done!





This is the test that I been used to guarantee the steps that the user will do and the same time guarantee that the content generated from the prawn is the expected.



Maybe this explanation is not enough to you understand, then, remember that you can see the entire example on Github!