Carrierwave 测试 - 清理或单独上传文件?
Carrierwave testing - clean up or separate file uploads?
我想要一些反馈and/or请帮忙。
我正在做这个测试
scenario 'can create a new post' do
attach_file('Image', 'spec/files/hello-world.png')
fill_in 'Caption', with: 'Hello World! This is the first post!'
click_button 'Create Post'
expect(page).to have_css("img[src*='hello-world.png']")
expect(page).to have_content('Post was successfully created')
end
本次测试通过 Carrierwave 将图片上传到 uploads/post/image/1/hello-world.png
,这在开发环境等方面可能是相同的路径,所以我想知道这是否会导致文件和帖子之间出现任何问题。
我的问题是我是否需要将每个环境中的文件上传分开,还是 Rails 可以在内部管理?
文件的存储位置是defined in a Carrierwave Uploader by the method store_dir
。如果需要将不同环境创建的文件分开,最简单的解决方法是在路径中添加Rails.env
:
def store_dir
@store_dir ||= File.join(
'public',
'uploads',
Rails.env,
model.class.table_name.to_s,
mounted_as.to_s,
model.id.to_s
)
end
这将创建如下路径:public/uploads/production/posts/image/1/image.png
我想要一些反馈and/or请帮忙。
我正在做这个测试
scenario 'can create a new post' do
attach_file('Image', 'spec/files/hello-world.png')
fill_in 'Caption', with: 'Hello World! This is the first post!'
click_button 'Create Post'
expect(page).to have_css("img[src*='hello-world.png']")
expect(page).to have_content('Post was successfully created')
end
本次测试通过 Carrierwave 将图片上传到 uploads/post/image/1/hello-world.png
,这在开发环境等方面可能是相同的路径,所以我想知道这是否会导致文件和帖子之间出现任何问题。
我的问题是我是否需要将每个环境中的文件上传分开,还是 Rails 可以在内部管理?
文件的存储位置是defined in a Carrierwave Uploader by the method store_dir
。如果需要将不同环境创建的文件分开,最简单的解决方法是在路径中添加Rails.env
:
def store_dir
@store_dir ||= File.join(
'public',
'uploads',
Rails.env,
model.class.table_name.to_s,
mounted_as.to_s,
model.id.to_s
)
end
这将创建如下路径:public/uploads/production/posts/image/1/image.png