使用rails minitest和capybara如何测试pdf页面是否创建没有错误?我认为检查status_code是最简单的,但不确定capybara是否支持这一点。
scenario "opens pdf without errors" do
visit progress_report_path(format: 'pdf')
page.must_have_status_code(200)
end
我知道capybara没有“must_have_status_code”方法......但是,这说明了我想要完成的事情。
如何测试rails minitest capybara中的状态代码?或者,如何测试pdf页面是否由prawn生成且没有错误?
答案 0 :(得分:5)
我用这个来测试PDF下载:
visit '/reports/summary.pdf'
page.status_code.must_equal 200
content_disposition = page.driver.response.headers['Content-Disposition']
content_disposition.must_include 'filename="summary.pdf"'
# the following assertions are mostly equivalent, pick what suits your style best
# I benchmarked them each 5 million times with a small pdf:
# starts_with, ends_with: 0.000046
# must_includes: 0.000080
# regex 0.000111
assert page.body.starts_with? "%PDF-1.4"
assert page.body.ends_with? "%EOF\n"
page.body.must_include '%PDF-1.4'
page.body.must_include '%EOF'
page.body.must_match /\A%PDF-1\.4.*%EOF\n\z/m