在测试环境中,如何通过URL访问ActiveStorage对象?

时间:2018-08-29 07:31:55

标签: ruby-on-rails rails-activestorage

给出一个带有ActiveStorage附件的模型

class MyObject
  has_one_attached :avatar
end

在开发环境中,我能够将化身作为StringIO对象检索。

obj = MyObject.new( { valid params } )
file = File.open( Rails.root.join( "spec/support/images/test_image.jpg" ))
obj.avatar.attach( io: file, filename: "test_image.jpg" )
obj.save

version = obj.avatar.variant( resize: '200x200>').processed
version_url = Rails.application.routes.url_helpers.url_for( version )
download = open(version_url)
download.class 
=> StringIO

当我尝试在测试环境中进行同样的思考时,open(version_url)返回

Errno::ECONNREFUSED Exception: Failed to open TCP connection to localhost:3000 (Connection refused - connect(2) for "localhost" port 3000)

有人在测试中成功下载了活动存储附件吗?我应该如何配置测试环境来实现这一目标?

我的测试环境已经有

config.active_storage.service = :test
Rails.application.routes.default_url_options = {host: "localhost:3000"}

我忽略了什么?

编辑

#storage.yml
test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

0 个答案:

没有答案