假设您有一个类似的 YAML 配置文件:
defaults: &defaults
# registration form
birth_date: true
address: true
zip: true
city: true
state: true
# other stuff
send_email_notification_to_users: true
production:
<<: *defaults
development:
<<: *defaults
test:
<<: *defaults
其加载方式与 Railcast #85 中的解释类似: http://railscasts.com/episodes/85-yaml-configuration-file
假设您需要测试应用程序在不同设置下的性能,您会如何做?
使用 Django,可以在单元测试期间临时更改设置: https://docs.djangoproject.com/en/dev/topics/testing/overview/#overriding-settings
Rails 是否可以做类似的事情?
最佳答案
如果您完全按照 Railscast #85 中的说明实现它,只需像这样分配新值即可:
APP_CONFIG['perform_authentication'] = false
# or
APP_CONFIG['my_fancy_key'] = 'my fancy value'
请记住,测试用例完成后该值不会自动改回,因此它将对所有后续测试用例保持有效。
关于ruby-on-rails - rails : change configuration loaded from YAML at runtime in unit/functional tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14563749/