开发者

rails 3: testing custom config variables

开发者 https://www.devze.com 2023-02-16 07:46 出处:网络
I want to write a few packaged plugins that I can pull into a project and configure for the specific project.For example, I may have a rolodex plugin in which i want the person\'s birthday to be confi

I want to write a few packaged plugins that I can pull into a project and configure for the specific project. For example, I may have a rolodex plugin in which i want the person's birthday to be configurable as required or not required.

in config/initializers/rolodex.rb

Rolodex::Application.config.require_birthday = true

Then in model Person.rb

class Person << ActiveRecord::Base

validates_presence_of :birthday, :if => Proc.new{ |p| Rolodex::Application.config.require_birthday}

...

a little bumpy for an approach maybe - if there's a more idiomatic way, holler.

but here's my question. i'm trying to write unit tests to verify that the config switch works. but i can't. if i write a unit test that sets ..require_birthday = false, it's too late, because the class has already been loaded and开发者_运维技巧 the validator defined based on the config values in the initializers. how am i supposed to test this?


it "should be strange"
 Rolodex::Application.config.should_receive(:require_birthday=).with(true)
 require Rails.root.join("config/initializers/rolodex")
end
0

精彩评论

暂无评论...
验证码 换一张
取 消