Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用Capybara进行测试时,Carrierwave找不到文件_Ruby On Rails_Capybara_Carrierwave - Fatal编程技术网

Ruby on rails 使用Capybara进行测试时,Carrierwave找不到文件

Ruby on rails 使用Capybara进行测试时,Carrierwave找不到文件,ruby-on-rails,capybara,carrierwave,Ruby On Rails,Capybara,Carrierwave,我正在尝试编写一个集成测试,它涉及一个用Carrierwave上传的文件。我有以下配置: CarrierWave.configure do |config| if Rails.env.test? config.storage = :file config.enable_processing = false else # other configs end end 我的上传程序的存储路径设置为: def store_dir

我正在尝试编写一个集成测试,它涉及一个用Carrierwave上传的文件。我有以下配置:

CarrierWave.configure do |config|
  if Rails.env.test?
    config.storage = :file
    config.enable_processing = false
  else
    # other configs
  end
end
我的上传程序的存储路径设置为:

def store_dir                                                                    
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"             
end
这样做的结果是该文件存储在public/uploads中。但是,当我试图在我的模型上通过doc.url方法访问此文件时,它返回一个类似/uploads/…的路径,并且在运行我的集成规范时,我得到以下错误:

unable to open file `/uploads/doc/paper/1/img_1.png'

我可以确认该文件确实存在于/public/uploads目录中。如何让Carrierwave返回上传图像的正确路径?我可以用Rails.root.join'public',doc.url将其修补在一起,但这会破坏我们上传到S3的生产。

在进一步研究之后,我意识到这里的问题是我们试图访问文件服务器端。客户端一切正常,因为它使用相对路径,Rails将其解析为资产路径。服务器端它不知道去哪里找。在开发和生产中,我们使用S3,因此url在两种方式中都是相同的。这感觉有点像黑客,但在需要访问服务器上的映像时,我们做到了以下几点:

path = Rails.env.test? ? doc.img.path : doc.url
file = File.open(path)

我无法找到一种与环境无关的方法来处理此问题。

期望任何实例并在URL上返回路径值是的,这就是我们在测试中为其中一些实例所做的。