测试fileupload的Rails收到错误“找不到文件”

时间:2012-12-09 12:08:16

标签: ruby-on-rails ruby-on-rails-3 rspec

我正在使用RSpec(Rails 3.2)来测试我的控制器。我有一个控制器,它还包含一个fileupload(使用CarrierWave),但我一直收到错误:

  

失败/错误:“image”=> fixture_file_upload(Rails.root.join( '规范',   'fixtures','files','image.png'),'image / png')        RuntimeError:          ... / spec / fixtures / files / image.png文件不存在

在我的控制器中,我已经像这样定义了图像上传:

  def valid_attributes
{ "title" => "My own title",
  "description" => "Something cool",
  "image" => fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'image.png'), 'image/png')
}

我当然检查过该文件是否存在,但可能还有其他一些我忽略的内容?

1 个答案:

答案 0 :(得分:4)

似乎你必须在Rails 3.2中使用Rack::Test::UploadedFile.new而不是fixture_file_upload

def valid_attributes
  { "title" => "My own title",
    "description" => "Something cool",
    "image" => Rack::Test::UploadedFile.new(Rails.root.join('spec', 'fixtures', 'files', 'image.png'), 'image/png')
  }
end

请参阅此SO问题/答案:fixture_file_upload has {file} does not exist error