如何在RSpec中测试send_data?或者......在这种情况下我应该测试什么?

use*_*369 13 rspec ruby-on-rails

使用RSpec测试以下代码的最佳方法是什么?我应该测试什么?show动作打开一个文件并对其进行流式传输.此外,如果操作依赖于某处存在的文件,我可以测试吗?

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
Run Code Online (Sandbox Code Playgroud)

She*_*aun 30

我需要send_data在下载csv文件的控制器操作中进行测试,我是按照以下方式完成的.

控制器:

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
Run Code Online (Sandbox Code Playgroud)

(rspec 2解决方案)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
Run Code Online (Sandbox Code Playgroud)

(rspec 3解决方案)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
Run Code Online (Sandbox Code Playgroud)

  • 我不推荐这个.模拟你不拥有的代码通常不是一个好主意 - 除其他外,在原始的OOPSLA模拟文件中明确警告它.如果`send_data`的API发生了变化(例如,使用Rails升级),您将无法知道您的代码是否已损坏. (3认同)
  • 对我来说,`什么都不渲染:true`是行不通的,它仍然不断抛出`“缺少模板”`错误。我通过调用原始文件解决了它:`expect(@controller).to receive(:send_data).with(csv_string, csv_options).and_call_original`。这感觉不像解决方案那么干净,但我认为实际发送带有文件名的响应不会对控制器规范造成任何伤害 (2认同)