You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails DirectUpload本地正常,GitHub Actions测试报‘Invalid URL’错误

DirectUpload单元测试本地正常,GitHub CI环境失败

报错详情

1) Users::PutAvatar when signed_blob_id exist return sucess
     Failure/Error: user.avatar.attach(blob)

     ArgumentError:
       Invalid URL: ""
     # ./app/services/users/put_avatar.rb:14:in `call'
     # ./spec/services/users/put_avatar_spec.rb:6:in `block (2 levels) in <main>'
     # ./spec/services/users/put_avatar_spec.rb:29:in `block (3 levels) in <main>'

已尝试的配置

config.hostname = URI(ENV.fetch('HOST_URL', 'http://localhost:3000')).hostname
routes.default_url_options[:host] = ENV.fetch('HOST_URL', 'http://localhost:3000')

可行排查方向及解决方案

  • 先确认GitHub CI环境中HOST_URL环境变量是否正确配置。可以在CI执行脚本里加一步echo $HOST_URL,看看输出是否为空——如果没设置这个变量,默认值是否正常生效?
  • 打开app/services/users/put_avatar.rb第14行,定位生成空URL的代码逻辑。DirectUpload的URL生成通常依赖Rails.application.routes.url_helpers,有可能测试环境没显式配置好default_url_options。
  • 在spec_helper.rb或rails_helper.rb里给测试环境单独配置,确保每个测试用例都能拿到正确的host:
    RSpec.configure do |config|
      config.before(:each) do
        Rails.application.routes.default_url_options[:host] = ENV.fetch('HOST_URL', 'http://localhost:3000')
      end
    end
    
  • 检查Active Storage的CI环境配置,确认config.active_storage.service设置为:test(测试环境默认配置),避免因存储服务配置错误导致URL生成失败。
  • 如果测试中用到了DirectUpload相关方法,确认测试前是否初始化了URL生成所需参数,比如手动设置blob的URL,或者确保签名URL生成时依赖的host参数正确。

内容的提问来源于stack exchange,提问作者comm1ted

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 02:34:56