我开发的系统使用 ActiveStorage 将缓存数据存储在 S3 上,然后再将其用于其他用途。在我的规范中,我想存根该文件的下载方法,并加载特定文件以进行测试。
allow(user.cached_data).to receive(:download)
.and_return(read_json_file('sample_data.json'))
(read_json_file
是一个规范助手File.read
then JSON.parse
一个数据文件。)
我收到此错误:
#<ActiveStorage::Attached::One:0x00007f9304a934d8 @name="cached_data",
@record=#<User id: 4, name: "Bob", email: "[email protected] /cdn-cgi/l/email-protection",
created_at: "2019-08-22 09:11:16", updated_at: "2019-08-22 09:11:16">,
@dependent=:purge_later> does not implement: download
我不明白,the docs https://edgeguides.rubyonrails.org/active_storage_overview.html#downloading-files明确地说这个对象应该实现下载。
Edit
正如建议的Jignesh https://stackoverflow.com/users/936494/jignesh-gohel and Stephen https://stackoverflow.com/users/680847/stephen-hanson,我尝试过这个:
allow(user.cached_data.blob).to receive(:download)
.and_return(read_json_file('sample_data.json'))
我收到以下错误:
Module::DelegationError:
blob delegated to attachment, but attachment is nil
user
是由 FactoryBot 生成的,所以我目前正在尝试附加我的cached_data
该对象的示例文件。
我的工厂是这样的:
FactoryBot.define do
factory :user
name { 'Robert' }
email { '[email protected] /cdn-cgi/l/email-protection' }
after(:build) do |user|
user.cached_data.attach(io: File.open("spec/support/sample_data.json"), filename: 'sample.json', content_type: 'application/json')
end
end
end
但是当我添加它时after build
阻止工厂,我收到以下错误:
ActiveRecord::LockWaitTimeout:
Mysql2::Error::TimeoutError: Lock wait timeout exceeded
也许这是另一个 Stackoverflow 问题。