我正在使用Cucumber和Capybara编写功能和步骤定义,我想将用户凭据存储在YAML文件中。
我的问题是,如果我的support / config.yml中有一个cred.yml文件,并且我在env.rb(CONFIG = YAML.load_file("/config/config.yml")
)中加载该文件,是否可以访问所有信息?如果是这样,我将如何从env_1访问/调用user1?
或者如果我想一次只加载一个/多个选择环境,我该怎么做?我如何访问/呼叫不同的用户?
这样的事情:
CONFIG = YAML.load_file("/config/config.yml")[ENV]
?
config.yml文件内容:
env_1:
`user1: admin`
`password1: password`
`user2: teacher`
`password2: password`
env_2:
`user: student`
`password: password`
`user2: assistant`
`password2: password`
答案 0 :(得分:4)
YAML::load_file
返回嵌套哈希:
require 'yaml'
config = YAML.load_file("config.yml") #=> {"env_1"=>{"user1"=>"admin", "password1"=>"password", "user2"=>"teacher", "password2"=>"password"}, "env_2"=>{"user"=>"student", "password"=>"password", "user2"=>"assistant", "password2"=>"password"}}
您可以通过以下方式访问env_1
config["env_1"] #=> {"user1"=>"admin", "password1"=>"password", "user2"=>"teacher", "password2"=>"password"}
其价值观:
config["env_1"]["user1"] #=> "admin"
config["env_1"]["user2"] #=> "teacher"
访问env_2
会相应地发挥作用:
config["env_2"]["user"] #=> "student"
假设您的config.yml
看起来像这样:
env_1:
user1: admin
password1: password
user2: teacher
password2: password
env_2:
user: student
password: password
user2: assistant
password2: password