2016-07-29 102 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类时仅检查一次。

在我的具体情况,这个代价是罚款,但其他人可能要实际回答我的问题。

相关问题