Module: Spree::TestingSupport::Preferences

Defined in:
lib/spree/testing_support/preferences.rb

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • preference_store_class (Class)

    the configuration class we want to freeze.



89
90
91
92
93
94
# File 'lib/spree/testing_support/preferences.rb', line 89

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



10
11
12
13
# File 'lib/spree/testing_support/preferences.rb', line 10

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

Yields:



6
7
8
# File 'lib/spree/testing_support/preferences.rb', line 6

def configure_spree_preferences
  yield(Spree::Config) 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.

Examples:

Stubs ‘currency` and `track_inventory_levels` on `Spree::Config`:

stub_spree_preferences(currency: 'EUR', track_inventory_levels: false)
expect(Spree::Config.currency).to eql 'EUR'

Stubs ‘locale` preference on `Spree::Backend::Config`:

stub_spree_preferences(Spree::Backend::Config, locale: 'fr'),
expect(Spree::Backend::Config.locale).to eql 'fr'

Parameters:

  • prefs_or_conf_class (Class, Hash)

    the class we want to stub preferences for or the preferences hash (see prefs param). If this param is an Hash, preferences will be stubbed on Spree::Config.

  • prefs (Hash, nil) (defaults to: nil)

    names and values to be stubbed

See Also:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spree/testing_support/preferences.rb', line 39

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.

Examples:

Temporarily switch to an unfrozen store and change some preferences:

with_unfrozen_spree_preference_store do
  Spree::Config.currency = 'EUR'
  Spree::Config.track_inventory_levels = false

  expect(Spree::Config.currency).to eql 'EUR'
end

See Also:



70
71
72
73
74
75
76
# File 'lib/spree/testing_support/preferences.rb', line 70

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