如何在 RSpec 中测试 send_data?或者……在这种情况下我应该测试什么? [英] How to test send_data in RSpec? Or...what should I be testing for in this case?
本文介绍了如何在 RSpec 中测试 send_data?或者……在这种情况下我应该测试什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 RSpec 测试以下代码的最佳方法是什么?我应该测试什么?show 操作打开一个文件并流式传输它.此外,如果该操作依赖于某处存在的文件,我可以对其进行测试吗?
def 显示image_option = params[:image_option]response_to do |格式|格式.jsformat.pdf {open_bmap_file("#{@bmap.bmap_pdf_file}", 'application/pdf', "#{@bmap.bmap_name}.pdf", "pdf", "pdf")}format.png {open_bmap_file("#{@bmap.bmap_png_file}", 'image/png', "#{@bmap.bmap_name}.png", "png", image_option)}结尾结尾私人的def open_bmap_file(filename, application_type, send_filename, format, image_option = nil)filename = "app/assets/images/image_not_available_small.png" 除非 File.exist?文件名路径 = Bmap.bmaps_pngs_path案例 image_option当形象"文件名 = "#{@bmap.bmap_name}.png"当大缩略图"filename = "#{@bmap.bmap_name}_large_thumb.png"当缩略图"文件名 = "#{@bmap.bmap_name}_thumb.png"当pdf"文件名 = "#{@bmap.bmap_name}.pdf"路径 = Bmap.bmaps_pdfs_path别的文件名 = "#{@bmap.bmap_name}.pdf"路径 = Bmap.bmaps_pdfs_path结尾开始File.open(path + filename, 'rb') do |f|send_data f.read, :disposition =>image_option == "pdf" ?附件":内联",:类型 =>application_type, :filename =>发送文件名结尾救援flash[:error] = '找不到文件.'重定向到 root_url结尾
解决方案
我需要在下载 csv 文件的控制器操作中测试 send_data
,我是通过以下方式完成的.
>
控制器:
def 索引response_to do |格式|format.csv 做发送数据(模型.generate_csv,类型:'文本/csv;字符集=utf-8;标题=存在',文件名:report.csv",处置:'附件')结尾结尾结尾
(rspec 2 解决方案)controller_spec:
context "when format is csv" dolet(:csv_string) { Model.generate_csv }让(:csv_options){{文件名:report.csv",处置:'附件',类型:'text/csv;字符集=utf-8;标题=存在'} }它应该返回一个csv附件"做@controller.should_receive(:send_data).with(csv_string, csv_options).and_return { @controller.render nothing: true } # 防止缺少模板"错误获取:索引,格式::csv结尾结尾
(rspec 3 解决方案)controller_spec:
context "when format is csv" dolet(:csv_string) { Model.generate_csv }让(:csv_options){{文件名:report.csv",处置:'附件',类型:'text/csv;字符集=utf-8;标题=存在'} }它应该返回一个csv附件"做期望(@controller).接收(:send_data).with(csv_string,csv_options){@controller.render nothing: true # 防止缺少模板"错误}获取:索引,格式::csv结尾结尾
What's the best way to test the following code using RSpec? And what should I be testing for? The show action opens a file and streams it. Also, if the action relies on a file existing somewhere, can I test that?
def show
image_option = params[:image_option]
respond_to do |format|
format.js
format.pdf {open_bmap_file("#{@bmap.bmap_pdf_file}", 'application/pdf', "#{@bmap.bmap_name}.pdf", "pdf", "pdf")}
format.png {open_bmap_file("#{@bmap.bmap_png_file}", 'image/png', "#{@bmap.bmap_name}.png", "png", image_option)}
end
end
private
def open_bmap_file(filename, application_type, send_filename, format, image_option = nil)
filename = "app/assets/images/image_not_available_small.png" unless File.exist? filename
path = Bmap.bmaps_pngs_path
case image_option
when "image"
filename = "#{@bmap.bmap_name}.png"
when "large_thumbnail"
filename = "#{@bmap.bmap_name}_large_thumb.png"
when "thumbnail"
filename = "#{@bmap.bmap_name}_thumb.png"
when "pdf"
filename = "#{@bmap.bmap_name}.pdf"
path = Bmap.bmaps_pdfs_path
else
filename = "#{@bmap.bmap_name}.pdf"
path = Bmap.bmaps_pdfs_path
end
begin
File.open(path + filename, 'rb') do |f|
send_data f.read, :disposition => image_option == "pdf" ? 'attachment' : 'inline', :type => application_type, :filename => send_filename
end
rescue
flash[:error] = 'File not found.'
redirect_to root_url
end
解决方案
I needed to test send_data
in a controller action that downloads a csv file, and I did it in the following way.
controller:
def index
respond_to do |format|
format.csv do
send_data(Model.generate_csv,
type: 'text/csv; charset=utf-8; header=present',
filename: "report.csv",
disposition: 'attachment')
end
end
end
(rspec 2 solution) controller_spec:
context "when format is csv" do
let(:csv_string) { Model.generate_csv }
let(:csv_options) { {filename: "report.csv", disposition: 'attachment', type: 'text/csv; charset=utf-8; header=present'} }
it "should return a csv attachment" do
@controller.should_receive(:send_data).with(csv_string, csv_options).
and_return { @controller.render nothing: true } # to prevent a 'missing template' error
get :index, format: :csv
end
end
(rspec 3 solution) controller_spec:
context "when format is csv" do
let(:csv_string) { Model.generate_csv }
let(:csv_options) { {filename: "report.csv", disposition: 'attachment', type: 'text/csv; charset=utf-8; header=present'} }
it "should return a csv attachment" do
expect(@controller).to receive(:send_data).with(csv_string, csv_options) {
@controller.render nothing: true # to prevent a 'missing template' error
}
get :index, format: :csv
end
end
这篇关于如何在 RSpec 中测试 send_data?或者……在这种情况下我应该测试什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文