Module: RailsStuff::RSpecHelpers

Extended by:
RSpecHelpers
Included in:
RSpecHelpers
Defined in:
lib/rails_stuff/rspec_helpers.rb,
lib/rails_stuff/rspec_helpers/signinable.rb,
lib/rails_stuff/rspec_helpers/concurrency.rb,
lib/rails_stuff/rspec_helpers/groups/feature.rb,
lib/rails_stuff/rspec_helpers/groups/request.rb,
lib/rails_stuff/rspec_helpers/matchers/redirect_with_turbolinks.rb

Overview

Collection of RSpec configurations and helpers for better experience.

Defined Under Namespace

Modules: Concurrency, Groups, Matchers, Signinable

Instance Method Summary collapse

Instance Method Details

#clear_logsObject

Clear logs ‘tail -f`-safely.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rails_stuff/rspec_helpers.rb', line 100

def clear_logs
  ::RSpec.configure do |config|
    config.add_setting :clear_log_file
    config.clear_log_file = Rails.root.join('log', 'test.log') if defined?(Rails.root)
    config.add_setting :clear_log_file_proc
    config.clear_log_file_proc = ->(file) do
      next unless file && File.exist?(file)
      FileUtils.cp(file, "#{file}.last")
      File.open(file, 'w').close
    end
    config.after(:suite) do
      instance_exec(config.clear_log_file, &config.clear_log_file_proc) unless ENV['KEEP_LOG']
    end
  end
end

#database_cleanerObject

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`.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_stuff/rspec_helpers.rb', line 45

def database_cleaner # rubocop:disable AbcSize
  return unless defined?(DatabaseCleaner)
  ::RSpec.configure do |config|
    if config.respond_to?(:use_transactional_fixtures=)
      config.use_transactional_fixtures = false
    end
    config.add_setting :database_cleaner_strategy
    config.database_cleaner_strategy = {feature: :truncation}
    config.database_cleaner_strategy.default = :transaction
    config.add_setting :database_cleaner_options
    config.database_cleaner_options = {truncation: {except: %w[spatial_ref_sys]}}
    config.add_setting :database_cleaner_args
    config.database_cleaner_args = ->(ex) do
      strategy = ex.[:concurrent] && :truncation
      strategy ||= config.database_cleaner_strategy[ex.[:type]]
      options = config.database_cleaner_options[strategy] || {}
      [strategy, options]
    end
    config.around do |ex|
      DatabaseCleaner.strategy = config.database_cleaner_args.call(ex)
      DatabaseCleaner.cleaning { ex.run }
    end
  end
end

#debugObject

Runs debugger after each failed example with ‘:debug` tag. Uses `pry` by default, this can be configured `config.debugger=`.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rails_stuff/rspec_helpers.rb', line 86

def debug
  ::RSpec.configure do |config|
    config.add_setting :debugger_proc
    config.debugger_proc = ->(ex) do
      exception = ex.exception
      defined?(Pry) ? binding.pry : debugger # rubocop:disable Debugger
    end
    config.after(debug: true) do |ex|
      instance_exec(ex, &config.debugger_proc) if ex.exception
    end
  end
end

#frozen_timeObject

Freeze time for specs with ‘:frozen_time` metadata.



117
118
119
120
121
# File 'lib/rails_stuff/rspec_helpers.rb', line 117

def frozen_time
  ::RSpec.configure do |config|
    config.around(frozen_time: true) { |ex| Timecop.freeze { ex.run } }
  end
end

#redisObject

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`.



73
74
75
76
77
78
79
80
81
82
# File 'lib/rails_stuff/rspec_helpers.rb', line 73

def redis
  ::RSpec.configure do |config|
    config.add_setting :redis
    config.redis = Rails.redis if defined?(Rails.redis)
    config.add_setting :flush_redis_proc
    config.flush_redis_proc = ->(*) { Array.wrap(config.redis).each(&:flushdb) }
    config.before(flush_redis: true) { instance_exec(&config.flush_redis_proc) }
    config.after(:suite) { instance_exec(&config.flush_redis_proc) }
  end
end

#setup(only: nil, except: nil) ⇒ Object

Single endpoint for multiple seups. Use ‘:only` and `:except` options to filter actions.



13
14
15
16
17
18
19
20
21
22
# File 'lib/rails_stuff/rspec_helpers.rb', line 13

def setup(only: nil, except: nil)
  items = instance_methods.map(&:to_s) - %w[setup]
  items -= Array.wrap(except).map(&:to_s) if except
  if only
    only = Array.wrap(only).map(&:to_s)
    items &= only
    items += only
  end
  items.uniq.each { |item| public_send(item) }
end

#test_helpersObject

Setup all TestHelpers.



35
36
37
# File 'lib/rails_stuff/rspec_helpers.rb', line 35

def test_helpers
  TestHelpers.setup
end