Module: Spree::TestingSupport::Preferences
- Defined in:
- lib/spree/testing_support/preferences.rb
Class Method Summary collapse
-
.freeze_preferences(preference_store_class) ⇒ Object
This class method allows to freeze preferences for a specific configuration store class.
Instance Method Summary collapse
- #assert_preference_unset(preference) ⇒ Object
- #configure_spree_preferences {|Spree::Config| ... } ⇒ Object
- #reset_spree_preferences(&config_block) ⇒ Object deprecated Deprecated.
-
#stub_spree_preferences(prefs_or_conf_class, prefs = nil) ⇒ Object
This is the preferred way for changing temporarily Spree preferences during tests via stubs, without changing the actual values stored in Spree::Config.
-
#with_unfrozen_spree_preference_store(preference_store_class: Spree::Config) ⇒ Object
This method allows to temporarily switch to an unfrozen Spree::Config preference store with all proper preferences values set.
Class Method Details
.freeze_preferences(preference_store_class) ⇒ Object
This class method allows to freeze preferences for a specific configuration store class. It also stores the current state into a new preference of that store, so it can be reused when needed (eg. with_unfrozen_spree_preference_store)
It is meant to be used by extensions as well, for example if one extension has its own Spree::ExtensionName::Config class, we can freeze it and be sure we always stub values on it during tests.
112 113 114 115 116 117 |
# File 'lib/spree/testing_support/preferences.rb', line 112 def self.freeze_preferences(preference_store_class) config_class = preference_store_class.class config_class.preference :unfrozen_preference_store, :hash preference_store_class.unfrozen_preference_store = preference_store_class.preference_store.dup preference_store_class.preference_store.freeze end |
Instance Method Details
#assert_preference_unset(preference) ⇒ Object
33 34 35 36 |
# File 'lib/spree/testing_support/preferences.rb', line 33 def assert_preference_unset(preference) find("#preferences_#{preference}")['checked'].should be false Spree::Config[preference].should be false end |
#configure_spree_preferences {|Spree::Config| ... } ⇒ Object
29 30 31 |
# File 'lib/spree/testing_support/preferences.rb', line 29 def configure_spree_preferences yield(Spree::Config) if block_given? end |
#reset_spree_preferences(&config_block) ⇒ Object
Resets all preferences to default values, you can pass a block to override the defaults with a block
reset_spree_preferences do |config|
config.track_inventory_levels = false
end
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/spree/testing_support/preferences.rb', line 16 def reset_spree_preferences(&config_block) Spree::Config.instance_variables.each { |iv| Spree::Config.remove_instance_variable(iv) } Spree::Config.preference_store = Spree::Config.default_preferences if defined?(Railties) Rails.application.config.spree = Spree::Config.environment end configure_spree_preferences(&config_block) if block_given? end |
#stub_spree_preferences(prefs_or_conf_class, prefs = nil) ⇒ Object
This is the preferred way for changing temporarily Spree preferences during tests via stubs, without changing the actual values stored in Spree::Config.
By using stubs no global preference change will leak outside the lifecycle of each spec example, avoiding possible unpredictable side effects.
This method may be used for stubbing one or more different preferences at the same time.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/spree/testing_support/preferences.rb', line 62 def stub_spree_preferences(prefs_or_conf_class, prefs = nil) if prefs_or_conf_class.is_a?(Hash) preference_store_class = Spree::Config preferences = prefs_or_conf_class else preference_store_class = prefs_or_conf_class preferences = prefs end preferences.each do |name, value| if preference_store_class.method(:[]).owner >= preference_store_class.class allow(preference_store_class).to receive(:[]).and_call_original end allow(preference_store_class).to receive(:[]).with(name) { value } allow(preference_store_class).to receive(name) { value } end end |
#with_unfrozen_spree_preference_store(preference_store_class: Spree::Config) ⇒ Object
This method allows to temporarily switch to an unfrozen Spree::Config preference store with all proper preferences values set.
It should be used sparingly, only when ‘stub_spree_preferences` would not work.
93 94 95 96 97 98 99 |
# File 'lib/spree/testing_support/preferences.rb', line 93 def with_unfrozen_spree_preference_store(preference_store_class: Spree::Config) frozen_store = preference_store_class.preference_store preference_store_class.preference_store = preference_store_class[:unfrozen_preference_store].dup yield ensure preference_store_class.preference_store = frozen_store end |