【问题标题】:How to test download image with send_file method with RSpec?如何使用带有 RSpec 的 send_file 方法测试下载图像?
【发布时间】:2016-05-30 16:44:15
【问题描述】:

我在测试图片下载时遇到了困难。

def download_img
  @image = Photo.find params[:id] unless params[:id].nil?
  @c = Cat.find params[:cat_id] unless params[:cat_id].nil?
  @foo = @image.foo unless @image.nil?

  send_file(Paperclip.io_adapters.for(@image.file).path, type: "jpeg", :disposition => "attachment", :filename => @image.name)
end

我的 RSpec 测试(控制器):

describe 'download_img' do
before do
  get :download_img, { id: image.id }
end

it 'retrieves image by id' do
  img = Photo.find image.id
  expect(img).not_to be_nil
end

it 'downloads image' do
  page.response_headers["Content-Type"].should == "application/jpg"
  page.response_headers["Content-Disposition"].should == "attachment; filename=\"image.name.jpg\""
end
end

当我为这两个测试运行 rspec 测试时,我得到一个错误:“没有这样的文件或目录@rb_sysopen - /home/public/system/photos/files/000/000/001/foo/IMG123.JPG”

谢谢。

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

    您在Photo.find image.id 中指的是来自固定装置的image 吗?请您检查是否有从灯具中引用的相应文件。我想您必须更改fixtures 中的路径并创建该文件,spec/fixtures 是一个好地方。

    【讨论】:

      猜你喜欢
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多