现在你的代码内容: 使用Ruby on Rails在Swagger中返回PDF的测试。

huangapple go评论85阅读模式
英文:

test returning pdf with swagger in ruby on rails

问题

我试图测试一个返回PDF文件的端点。

我的操作如下:

  1. def pdf
  2. respond_to do |format|
  3. format.pdf do
  4. @pdf = ApplicationController.render pdf: some_number,
  5. template: "show",
  6. layout: nil
  7. send_data(@pdf, filename: "something.pdf", type: "application/pdf", disposition: 'attachment')
  8. end
  9. end
  10. end

我的测试代码如下:

  1. path "/pdf" do
  2. get "return PDF" do
  3. produces 'application/pdf'
  4. response '200', 'PDF generated' do
  5. schema type: :file
  6. before do |example|
  7. sign_in @some_user
  8. submit_request(example.metadata)
  9. end
  10. it 'returns a 200 response' do |example|
  11. assert_response_matches_metadata(example.metadata)
  12. end
  13. end
  14. end
  15. end

但我收到错误消息:ActionController::UnknownFormat

我正在使用:ruby 3.1.0rails 7.0.0rswag 2.8.0openapi 3.0.1

有人能帮我解决问题吗?正确的测试PDF返回的方法是什么?

英文:

I'm trying to test an endpoint that returns a pdf file.

My action looks like this:

  1. def pdf
  2. respond_to do |format|
  3. format.pdf do
  4. @pdf = ApplicationController.render pdf: some_number,
  5. template: "show",
  6. layout: nil
  7. send_data(@pdf, :filename => "something.pdf", :type => "application/pdf", disposition: 'attachment')
  8. end
  9. end
  10. end

and my test code looks like this:

  1. path "/pdf" do
  2. get "return PDF" do
  3. produces 'application/pdf'
  4. response '200', 'PDF generated' do
  5. schema type: :file
  6. before do |example|
  7. sign_in @some_user
  8. submit_request(example.metadata)
  9. end
  10. it 'returns a 200 response' do |example|
  11. assert_response_matches_metadata(example.metadata)
  12. end
  13. end
  14. end
  15. end

but I'm getting the error: ActionController::UnknownFormat

I'm using: ruby 3.1.0, rails 7.0.0, rswag 2.8.0, openapi 3.0.1

Can anyone help me with my issue? and what is the correct way to test returning pdf?

答案1

得分: 0

我通过在URL中添加“.pdf”来解决了这个问题:

  1. path "/pdf.pdf" do
  2. ...
  3. end
英文:

Simply I solved the issue by adding .pdf to the URL:

  1. path "/pdf.pdf" do
  2. ...
  3. end

huangapple
  • 本文由 发表于 2023年4月6日 21:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定