【发布时间】:2018-04-19 09:50:11
【问题描述】:
我需要检查上传后文件是否已更改(占位符徽标已替换为真实徽标,所以我不能只使用 file.present?)
我有一个可以在真实文件上运行 md5 哈希的代码,但是我在通过 rspec 测试访问这个文件时遇到了问题。
RSpec 在测试期间将文件放在哪里?假设我有一个带有
的模型mount_uploader :logo
在我的规范中,我正在尝试做
it 'updates with a real logo' do
logo_absolute_path = File.join(Rails.root, model.logo.url)
expect { request }.to change_file(clean_path)
end
但是File.join(Rails.root, model.logo.url) 提出以下问题
没有这样的文件或目录@rb_sysopen - /Users/Me/dev/example/uploads/model/5ad86444aba9cf23416700f0/logo.png
所以我猜这不是在测试模式下存储文件的地方。谁能告诉我如何生成本地绝对文件路径来测试测试环境中的内容?
注意:expect { request }.to change_file(clean_path) 是基于 https://gist.github.com/mattwynne/736421 的匹配器
编辑我的匹配器的代码,因为它可能很重要:
RSpec::Matchers.define(:change_file) do |file_path|
supports_block_expectations
match do |procedure|
before_hash = md5_hash(file_path)
procedure.call
after_hash = md5_hash(file_path)
expect(before_hash).not_to eq(after_hash)
end
def md5_hash(file_path)
Digest::MD5.hexdigest(File.read(file_path))
end
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-5 carrierwave rspec-rails rspec3