I am developing a rails 3.2 application with which users can download PDF files. I really like test-based development using rspec and matts, but I don't understand this.
I have the following code inside my controller:
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
This is a pretty simple code, all it does is set up my PDFKit and load the created PDF file. Now I want to check all of this, including:
- Assigning instance variables (easy, of course, and it works)
- Sending data, i.e. rendering pdf => And this is where I got stuck
I tried the following:
controller.should_receive(:send_data)
but it gives me
Failure/Error: controller.should_receive(:send_data) (#<InvoicesController:0x007fd96fa3e580>).send_data(any args) expected: 1 time received: 0 times
Does anyone know how to check if a pdf file is really uploaded / sent? Also, what else do you see that you need to test for good testing? For example, testing a data type, i.e. an application / pdf, would be nice.
Thanks!
ruby-on-rails pdf wkhtmltopdf pdfkit rspec
weltschmerz
source share