Rails测试的自定义初始化变量

时间:2011-03-01 17:10:51

标签: ruby-on-rails

我有一个自定义初始化程序,用于配置“云”存储提供程序(Google,AWS,Rackspace)。我正在创建一个我想要由各种开发人员(使用不同的存储提供商)部署的应用程序,我希望能够单独测试每个云提供商。目前,我运行初始化程序文件三次并在运行rake test之前评论/取消注释每个提供程序。是否可以将选项传递给rake test然后我可以在我的初始化程序中用于控制流程(即加载特定的初始化程序)?类似的东西:

rake test --attached aws
rake test --attached google
rake test --attached rackspace

以下是我的初始化程序:

# config/initializers/attached.rb

Attached::Attachment.options[:medium] = :aws
Attached::Attachment.options[:credentials] = "#{Rails.root}/config/aws.yml"

Attached::Attachment.options[:medium] = :google
Attached::Attachment.options[:credentials] = "#{Rails.root}/config/google.yml"

Attached::Attachment.options[:medium] = :rackspace
Attached::Attachment.options[:credentials] = "#{Rails.root}/config/rackspace.yml"

谢谢!

1 个答案:

答案 0 :(得分:2)

我建议使用env-vars。

$ ATTACHED="aws" rake test

然后

# config/initializers/attached.rb

attached = ENV['ATTACHED'] || "aws"

case attached
when "aws"
  Attached::Attachment.options[:medium] = :aws
  Attached::Attachment.options[:credentials] = "#{Rails.root}/config/aws.yml"
...