Your best ways to test your PDFs #154
Replies: 2 comments 2 replies
-
This is pretty much it. In Prawn we have a test for the generated manual. We use hashes instead of full documents though. That way we don't have to keep a copy of the file in the repo. One caveat is that while Prawn does make some effort to make documents stable it's not a stated goal. For example, we need to have a hash per supported Ruby implementation because implementations have minute differences that result in insignificant differences but differences nonetheless. For example, JRuby's gzip compression produces different output to MRI. It's still valid just not byte-identical. |
Beta Was this translation helpful? Give feedback.
-
Thank you, I'll go to sleep less ignorant tonight! And what hashing function do you generally use? require 'digest/md5'
def test_is_same_document
# --- test ---
actual_book_path = build_a_nice_pdf_and_return_path(with_data)
# --- check ---
# Recorded hash
expected_hash = get_hash_for(this_test)
# => "f8cde659ca0708071f889d6bd5c296ad"
actual_hash = Digest::MD5.file(actual_book_path).hexdigest
assert_equal(expected_hash, actual_hash)
end |
Beta Was this translation helpful? Give feedback.
-
I would very much like to know what you consider to be the best way to test your output with Prawn/Prawn-table.
Personally, I haven't found anything better, especially nothing simpler than the
FileUtils::identical?
method.Let's say I have a piece of code that generates an invoice. I refine it until it produces the expected document.
Once I've precisely obtained this document, I name it
expected.pdf
and set it aside.Then, in my test that evaluates this piece of code, I generate a document that I call
actual.pdf
. All I need to do then is make sure that the two documents are identical.Beta Was this translation helpful? Give feedback.
All reactions