英文:
test returning pdf with swagger in ruby on rails
问题
我试图测试一个返回PDF文件的端点。
我的操作如下:
def pdf
respond_to do |format|
format.pdf do
@pdf = ApplicationController.render pdf: some_number,
template: "show",
layout: nil
send_data(@pdf, filename: "something.pdf", type: "application/pdf", disposition: 'attachment')
end
end
end
我的测试代码如下:
path "/pdf" do
get "return PDF" do
produces 'application/pdf'
response '200', 'PDF generated' do
schema type: :file
before do |example|
sign_in @some_user
submit_request(example.metadata)
end
it 'returns a 200 response' do |example|
assert_response_matches_metadata(example.metadata)
end
end
end
end
但我收到错误消息:ActionController::UnknownFormat
。
我正在使用:ruby 3.1.0
,rails 7.0.0
,rswag 2.8.0
,openapi 3.0.1
。
有人能帮我解决问题吗?正确的测试PDF返回的方法是什么?
英文:
I'm trying to test an endpoint that returns a pdf file.
My action looks like this:
def pdf
respond_to do |format|
format.pdf do
@pdf = ApplicationController.render pdf: some_number,
template: "show",
layout: nil
send_data(@pdf, :filename => "something.pdf", :type => "application/pdf", disposition: 'attachment')
end
end
end
and my test code looks like this:
path "/pdf" do
get "return PDF" do
produces 'application/pdf'
response '200', 'PDF generated' do
schema type: :file
before do |example|
sign_in @some_user
submit_request(example.metadata)
end
it 'returns a 200 response' do |example|
assert_response_matches_metadata(example.metadata)
end
end
end
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”来解决了这个问题:
path "/pdf.pdf" do
...
end
英文:
Simply I solved the issue by adding .pdf
to the URL:
path "/pdf.pdf" do
...
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论