Module: RailsStuff::TestHelpers::Configurator

Defined in:
lib/rails_stuff/test_helpers/configurator.rb

Overview

Collection of useful RSpec configurations.

RailsStuff::TestHelpers::Configurator.tap do |configurator|
  configurator.database_cleaner(config)
  # ...
end

Class Method Summary collapse

Class Method Details

.database_cleaner(config) ⇒ Object

Setups database cleaner to use strategy depending on metadata. By default it uses ‘:transaction` for all examples and `:truncation` for features and examples with `concurrent: true`.

Other types can be tuned with ‘config.cleaner_strategy` hash & `config.cleaner_strategy.default`.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rails_stuff/test_helpers/configurator.rb', line 19

def database_cleaner(config)
  config.use_transactional_fixtures = false
  config.add_setting :cleaner_strategy
  config.cleaner_strategy = {feature: :truncation}
  config.cleaner_strategy.default = :transaction
  config.around do |ex|
    strategy = ex.[:concurrent] && :truncation
    strategy ||= config.cleaner_strategy[ex.[:type]]
    options = strategy == :truncation ? {except: %w(spatial_ref_sys)} : {}
    DatabaseCleaner.strategy = strategy, options
    DatabaseCleaner.cleaning { ex.run }
  end
end

.debug(config, filter: {debug: true}, debugger: :pry) ⇒ Object

Runs debugger after each failed example. Uses ‘pry` by default and runs only for examples with `:debug` tag. This can be configured with `:debugger` and `:filter` options respectively:

configurator.debug(config, filter: {my_tag: :val}, debugger: true)
# to disable filter:
configurator.debug(config, filter: nil)


50
51
52
53
54
55
56
57
# File 'lib/rails_stuff/test_helpers/configurator.rb', line 50

def debug(config, filter: {debug: true}, debugger: :pry)
  config.after(filter) do |ex|
    if ex.exception
      debugger == :pry ? binding.pry : self.debugger # rubocop:disable Debugger
      ex.exception # noop
    end
  end
end

.redis(config) ⇒ Object

Setups redis to flush db after suite and before each example with ‘flush_redis: :true`. `Rails.redis` client is used by default. Can be tuned with `config.redis`.



36
37
38
39
40
41
# File 'lib/rails_stuff/test_helpers/configurator.rb', line 36

def redis(config)
  config.add_setting :redis
  config.redis = Rails.redis if defined?(Rails.redis)
  config.before { |ex| config.redis.flushdb if ex.[:flush_redis] }
  config.after(:suite) { config.redis.flushdb }
end