[英]Run an rspec “before” block before rails initializers run
我想before
block before
運行rspec before
以在Rails初始化程序運行之前進行一些設置,以便可以測試初始化程序應該做什么。 這可能嗎?
如果初始化程序中的邏輯足夠復雜,應對其進行測試,則應將其提取到一個幫助程序中,以便您可以隔離和測試該幫助程序,而無需使用初始化程序的上下文。
complex_initializer.rb
config.database.foo = calculate_hard_stuff()
config.database.bar = other_stuff()
您可以將其提取到可測試的幫助程序中(lib / config / database.rb)
module Config::DatabaseHelper
def self.generate_config
{:foo => calculate_hard_stuff, :bar => other_stuff)
end
def calculate_hard_stuff
# Hard stuff here
end
end
...然后只需在初始化程序中連接配置數據
db_config_values = Config::DatabaseHelper.generate_config
config.database.foo = db_config_values[:foo]
config.database.bar = db_config_values[:bar]
...並在單獨的測試中測試復雜的配置確定/計算,您可以在其中隔離輸入。
describe Config::DatabaseHelper do
describe '.calculate_hard_stuff' do
SystemValue.stubs(:config => value)
Config::DatabaseHelper.calculate_hard_stuff.should == expected_value
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.