3
環境:Rails 3.1.1 and Rspec 2.10.1
我正在通過外部YAML文件加載所有應用程序配置。我的初始化(config/initializers/load_config.rb)
看起來像這樣如何在rails 3中模擬/存根配置初始化程序哈希
AppConfig = YAML.load_file("#{RAILS_ROOT}/config/config.yml")[RAILS_ENV]
我的YAML文件位於下的config/config.yml
development:
client_system: SWN
b2c_agent_number: '10500'
advocacy_agent_number: 16202
motorcycle_agent_number: '10400'
tso_agent_number: '39160'
feesecure_eligible_months_for_monthly_payments: 1..12
test:
client_system: SWN
b2c_agent_number: '10500'
advocacy_agent_number: 16202
motorcycle_agent_number: '10400'
tso_agent_number: '39160'
feesecure_eligible_months_for_monthly_payments: 1..11
我訪問這些值,例如AppConfig['feesecure_eligible_months_for_monthly_payments']
在我的測試中一個我需要AppConfig['feesecure_eligible_months_for_monthly_payments']
才能返回不同的值,但不知道如何完成此操作。我嘗試以下方法,沒有運氣
describe 'monthly_option_available?' do
before :each do
@policy = FeeSecure::Policy.new
@settlement_breakdown = SettlementBreakdown.new
@policy.stub(:settlement_breakdown).and_return(@settlement_breakdown)
@date = Date.today
Date.should_receive(:today).and_return(@date)
@config = mock(AppConfig)
AppConfig.stub(:feesecure_eligible_months_for_monthly_payments).and_return('1..7')
end
.....
end
在我的各階級,我做這樣的事情
class Policy
def eligible_month?
eval(AppConfig['feesecure_eligible_months_for_monthly_payments']).include?(Date.today.month)
end
....
end
可有人請點我在正確的方向!
謝謝弗雷德裏克! – MMinhas
你會如何存留AppConfig ['foo'] ['bar']? – MMinhas
最簡單的事情可能是做與以前完全相同的存根,但不是返回值爲3,使它成爲'{'bar'=> 3}' –