Module: Spree::TestingSupport::Preferences

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

Instance Method Summary collapse

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

Yields:



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

Deprecated.

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(preferences) ⇒ 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` preferences

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

Parameters:

  • preferences (Hash)

    names and values to be stubbed

See Also:



55
56
57
58
59
60
61
62
63
# File 'lib/spree/testing_support/preferences.rb', line 55

def stub_spree_preferences(preferences)
  preferences.each do |name, value|
    if Spree::Config.method(:[]).owner >= Spree::Config.class
      allow(Spree::Config).to receive(:[]).and_call_original
    end
    allow(Spree::Config).to receive(:[]).with(name) { value }
    allow(Spree::Config).to receive(name) { value }
  end
end

#with_unfrozen_spree_preference_storeObject

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:



78
79
80
81
82
83
84
# File 'lib/spree/testing_support/preferences.rb', line 78

def with_unfrozen_spree_preference_store
  frozen_store = Spree::Config.preference_store
  Spree::Config.preference_store = Spree::Config[:unfrozen_preference_store].dup
  yield
ensure
  Spree::Config.preference_store = frozen_store
end