Question

I'm trying to test specific content inside a pdf document which is being generated via the prawn gem and attached to an ActionMailer email. I'm using RSpec and Capybara for testing.

I've managed to test the filename like this expect(ActionMailer::Base.deliveries[0].filename).to eq("my_file.pdf")

I thought that I've read somewhere that I have to test the pdf itself like this but it doesn't work. `expect(ActionMailer::Base.deliveries[0].body.encoded).to have_content(user.first_name)``

When running the test, I get the following error:

Failure/Error: expect(ActionMailer::Base.deliveries[0].body.encoded).to have_content(user.first_name)
       expected to find text "John" in "JVBERi0xLjQKJf////8KMSAwIG9iago8PCAvQ3JlYXRvciA8ZmVmZjAwNTAw MDcyMDA2MTAwNzcwMDZlPgovUHJvZHVjZXIgPGZlZmYwMDUwMDA3MjAwNjEw MDc3MDA2ZT4KPj4KZW5kb2JqCjIgMCBvYmoKPDwgL1R5cGUgL0NhdGFsb2cK L1BhZ2VzIDMgMCBSCj4+CmVuZG9iagozIDAgb2JqCjw8IC9UeXBlIC9QYWdl cwovQ291bnQgMQovS2lkcyBbNSAwIFJdCj4+CmVuZG9iago0IDAgb2JqCjw8 IC9MZW5ndGggMzE2NAo+PgpzdHJlYW0KcQpxCi9UcjEgZ3MKMjA2LjAwMCA2 NTYuMDAwIDIwMC4wMDAgMTAwLjAwMCByZQpTClEKCnEKNDAwLjAwMCAwIDAg NTAuMDAwIDEwNi4wMDAgNzA2LjAwMCBjbQovSTEgRG8KUQpxCi9UcjEgZ3MK MjA2LjAwMCA2NTYuMDAwIDIwMC4wMDAgMTAwLjAwMCByZQpTClEKCkJUCjIx OC40ODI1NTg1OTM3NSA2NDIuODk2IFRkCi9GMS4wIDE4IFRmCls8NTQ+IDEx MC44Mzk4NDM3NSA8NjU2OTZjNmU2MTY4NmQ2NTYyNjU3Mzc0OGE3NDY5Njc3 NTZlNjc+XSBUSgpFVAoKCkJUCjM2IDYyMy4zMjk5OTk5OTk5OTk5IFRkCkVU CgoKQlQKMzYgNjAzLjc2Mzk5OTk5OTk5OTkgVGQKRVQKCgpCVAoyNDguODUy

This text continues a long time.

Maybe it's just me, but this doesn't exactly look "testable" to me. Does someone know to do this? Thanks!

Was it helpful?

Solution

Firstly, your PDF-generation code should really be separate from your mailer code, and should have its own tests separate from your mailer tests. Please do this first.

Once you've separated your PDF-generation code and tests you can generate your PDF and test its content using the pdf-inspector gem, which is helpfully maintained by the same folks who make Prawn. Then in your mailer tests you can simply check whether the file is attached, using something like this.

P.S. The reason the email content looks garbled (JVBERi0xLjQ...) is that email attachments are (usually) Base64-encoded. But even if you decoded it, you might not be able to search the PDF content for a plaintext string without a library like pdf-inspector because it might be compressed (I don't know if Prawn does this by default). But really, your PDF code and tests and your email code and tests should be completely separate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top