我如何在carrierwave上传中使用填充rails fixtures(yaml)?文档似乎没有涉及到这一点,carrierwave wiki也没有。
我试过了
并且我已经验证了上面的ruby代码生成了一个有效的file对象。
发布于 2015-08-21 20:18:52
我怀疑fixture会有什么用武之地,因为它们只在Rails代码库中使用。再加上我使用它们,所以这才是最重要的。
我也发现了这个article,它帮助了我,也帮助了你。
简而言之就是test_helper.rb
class ActiveSupport::TestCase
# Add more helper methods to be used by all tests here...
CarrierWave.root = Rails.root.join('test/fixtures/files') #path is up to you
def after_teardown
super
CarrierWave.clean_cached_files!(0)
end
end
class CarrierWave::Mount::Mounter
def store!
# Not storing uploads in the tests
end
end然后我在我的上传程序中放入:
def store_dir
if Rails.env.test?
"images"
else
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end我确信我可以摆脱对store_dir三元like语句的需要,但现在它是有效的。如果这对你不起作用,我有另外一种方法,但这更干净。
https://stackoverflow.com/questions/24976594
复制相似问题