Skip to content

Instantly share code, notes, and snippets.

@dramsay
Last active November 8, 2019 22:08
Show Gist options
  • Save dramsay/c8da66f073a41a23cd58ec3ffd7190bd to your computer and use it in GitHub Desktop.
Save dramsay/c8da66f073a41a23cd58ec3ffd7190bd to your computer and use it in GitHub Desktop.
Rspec configuration
unless ENV['NO_COVERAGE']
require 'simplecov'
SimpleCov.start do
add_filter ".bundle"
end
end
require 'capybara/rspec'
require 'factory_bot_rails'
require 'webmock/rspec'
require 'paper_trail/frameworks/rspec'
require 'feature_helpers'
require "pundit/rspec"
WebMock.disable_net_connect!(allow_localhost: true)
# See https://robots.thoughtbot.com/headless-feature-specs-with-chrome
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(chromeOptions: { args: %w{window-size=1280,1024} }))
end
Capybara.register_driver :headless_chrome do |app|
# List of chromium switches: http://peter.sh/experiments/chromium-command-line-switches/
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(chromeOptions: { args: %w{headless window-size=1920,1024} })
)
end
Capybara.register_driver :headless_chrome_es do |app|
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(chromeOptions: { args: %w{headless window-size=1920,1024 lang=es} })
)
end
if ENV['WITH_CHROME']
Capybara.javascript_driver = :chrome
else
Capybara.javascript_driver = :headless_chrome
end
Capybara.default_max_wait_time = 4
Capybara.default_host = 'http://apply.example.com'
Capybara.server = :puma, { Silent: true }
module ScrollToHelper
def scroll_to(element)
script = <<-JS
arguments[0].scrollIntoView(true);
JS
Capybara.current_session.driver.browser.execute_script(script, element.native)
end
end
# https://robots.thoughtbot.com/automatically-wait-for-ajax-with-capybara
module WaitForAjax
def wait_for_ajax
Timeout.timeout(Capybara.default_max_wait_time) do
loop until finished_all_ajax_requests?
end
end
def finished_all_ajax_requests?
page.evaluate_script('jQuery.active').zero?
end
end
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
# compile front-end and load manifest
webpacker = Webpacker::Instance.new
Webpacker::Manifest.new(webpacker).refresh
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each, type: :feature) do
# :rack_test driver's Rack app under test shares database connection
# with the specs, so continue to use transaction strategy for speed.
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if !driver_shares_db_connection_with_specs
# Driver is probably for an external browser with an app
# under test that does *not* share a database connection with the
# specs, so use truncation strategy.
DatabaseCleaner.strategy = :truncation
end
end
config.before(:each) do
DatabaseCleaner.start
end
config.before(type: :feature) { ensure_tos_exists }
config.append_after(:each) do
DatabaseCleaner.clean
ActionMailer::Base.deliveries.clear
end
config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.example_status_persistence_file_path = "spec/examples.txt"
config.disable_monkey_patching!
if config.files_to_run.one?
config.default_formatter = 'doc'
end
config.profile_examples = nil
config.order = :random
Kernel.srand config.seed
config.include FactoryBot::Syntax::Methods
config.include WaitForAjax, type: :feature
config.include ScrollToHelper, type: :feature
config.include FeatureHelpers, type: :feature
def bbopen
save_and_open_screenshot
byebug
end
def login_as(customer)
# login code
end
def admin_login_as(user=nil)
# login code
end
def restoring_env(key, new_value)
before_value = ENV[key]
ENV[key] = new_value
yield
ensure
ENV[key] = before_value
end
end
RSpec::Matchers.define_negated_matcher :not_change, :change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment