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



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/integration_tests_rails/capybara/util.rb', line 50

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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/integration_tests_rails/capybara/util.rb', line 37

def configure_rspec
  RSpec.configure do |config|
    config.before(:each, type: :feature) do
      ::Capybara.current_driver = ::Capybara.javascript_driver
      IntegrationTestsRails::Capybara::Util.ensure_server_ready(self)
    end

    if IntegrationTestsRails.configuration.experimental_features
      config.include(Helper, type: :feature, unit: true)
    end
  end
end

.configure_webmockObject



11
12
13
14
# File 'lib/integration_tests_rails/capybara/util.rb', line 11

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

.ensure_server_ready(context) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/integration_tests_rails/capybara/util.rb', line 16

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



68
69
70
# File 'lib/integration_tests_rails/capybara/util.rb', line 68

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

.verbose?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/integration_tests_rails/capybara/util.rb', line 64

def verbose?
  IntegrationTestsRails.configuration.verbose
end