Module: IntegrationTestsRails::Capybara::Util

Defined in:
lib/integration_tests_rails/capybara/util.rb

Overview

Utilities for Capybara setup and configuration are found here.

Class Method Summary collapse

Class Method Details

.configure_routesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/integration_tests_rails/capybara/util.rb', line 18

def configure_routes
  return unless IntegrationTestsRails.configuration.experimental_features

  app = Rails.application
  routes = app.routes
  # Use append and let Rails handle finalization automatically
  routes.append do
    get '/tests', to: 'tests#index', as: :tests
  end
  routes.instance_variable_set(:@finalized, false)
  routes.finalize!
  log 'Routes appended.'
end

.configure_rspecObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/integration_tests_rails/capybara/util.rb', line 61

def configure_rspec
  RSpec.configure do |config|
    config.include Dsl, type: :feature
    configure_before_hook(config)
    configure_around_hook(config)
    if IntegrationTestsRails.configuration.experimental_features
      config.include(Helper, type: :feature,
                             unit: true)
    end
  end
end

.configure_webmockObject



13
14
15
16
# File 'lib/integration_tests_rails/capybara/util.rb', line 13

def configure_webmock
  WebMock.disable_net_connect!(allow_localhost: true)
  log 'WebMock configured to allow localhost connections'
end

.ensure_server_ready(context) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/integration_tests_rails/capybara/util.rb', line 40

def ensure_server_ready(context)
  return if @server_ready

  log "Waiting for server on #{::Capybara.app_host.presence || 'localhost'} to start..."
  configuration = IntegrationTestsRails.configuration
  server_retries = configuration.max_server_retries
  server_retries.times do |attempt|
    if configuration.experimental_features
      context.visit('/tests')
    else
      context.visit('/')
    end
    @server_ready = true
    log 'Server is ready!'
    break
  rescue StandardError
    log "Server not ready (attempt #{attempt + 1}/#{server_retries})."
  end
  log "Server did not start after #{server_retries} attempts..." unless @server_ready
end

.log(message) ⇒ Object



36
37
38
# File 'lib/integration_tests_rails/capybara/util.rb', line 36

def log(message)
  puts "[CAPYBARA] #{message}" if verbose?
end

.verbose?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/integration_tests_rails/capybara/util.rb', line 32

def verbose?
  IntegrationTestsRails.configuration.verbose
end