2016-07-29 35 views
1

我有一個模型,其行爲應該根據配置文件稍微改變。理論上,配置文件將爲我的客戶端的每次安裝應用程序進行更改。那麼我該如何測試這些變化?如何在Rails中測試不同的應用程序配置?

例如...

# in app/models/person.rb 

before_save automatically_make_person_contributer if Rails.configuration.x.people['are_contributers_by_default'] 



# in test/models/person_test.rb 

test "auto-assigns role if it should" do 
    # this next line doesn't actually work when the Person#before_save runs... 
    Rails.configuration.x.people['are_contributers_by_default'] = true 
end 

test "won't auto assign a role if it shouldn't" do 
    # this next line doesn't actually work when the Person#before_save runs... 
    Rails.configuration.x.people['are_contributers_by_default'] = false 
end 

它沒有意義,這些被存儲在數據庫中,因爲他們是一個時間的配置,但我需要確保在所有的我的應用程序的行爲所有環境中可能的配置。

回答

1

看起來像這樣做的工作是重寫Person類,以便automatically_make_person_contributer實際上執行Rails.configuration.x.people['are_contributers_by_default']的評估。這使得我的測試中快樂和技術上不改變應用程序的工作方式:

# in app/models/person.rb 

before_save :automatically_make_person_contributer 

private 
    def automatically_make_person_contributer 
    if Rails.configuration.x.people['are_contributers_by_default'] 
     # do the actual work here 
    end 
    end 

然而,這意味着將要保持相同的應用程序的過程的生命週期值每次都會被檢查創建一個Person,而不是在創建Person類時僅檢查一次。

在我的具體情況,這個代價是罰款,但其他人可能要實際回答我的問題。

相關問題