我正在测试一个返回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
添加到URL来解决这个问题:
path "/pdf.pdf" do
...
end