From c36423e53a6fe9aff5838870d57b591a1ac64c61 Mon Sep 17 00:00:00 2001 From: ZippyDev Date: Sun, 22 Mar 2020 09:44:05 -0600 Subject: [PATCH] add rspec to template. --- .gitignore | 2 + .rspec | 1 + .rubocop.yml | 1 + README.md | 8 +- app/admin/dashboard.rb | 16 +-- config/application.rb | 2 +- ...0319234126_create_active_admin_comments.rb | 2 + spec/factories/users.rb | 2 +- spec/rails_helper.rb | 67 ++++++++++++ spec/spec_helper.rb | 101 ++++++++++++++++++ spec/support/capybara_driver.rb | 14 +++ spec/support/database_cleaner.rb | 52 +++++++++ spec/support/shoulda_matchers.rb | 10 ++ spec/support/validates_email_format.rb | 3 + spec/support/wait_for_ajax.rb | 21 ++++ 15 files changed, 292 insertions(+), 10 deletions(-) create mode 100644 .rspec create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/support/capybara_driver.rb create mode 100644 spec/support/database_cleaner.rb create mode 100644 spec/support/shoulda_matchers.rb create mode 100644 spec/support/validates_email_format.rb create mode 100644 spec/support/wait_for_ajax.rb diff --git a/.gitignore b/.gitignore index 777a4ea..169aefa 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,5 @@ /yarn-error.log yarn-debug.log* .yarn-integrity + +coverage diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/.rubocop.yml b/.rubocop.yml index fb87b3f..ed05446 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -8,6 +8,7 @@ AllCops: Exclude: - '**/*.jbuilder' - 'node_modules/**/*' + - 'db/schema.rb' Documentation: Enabled: false diff --git a/README.md b/README.md index 6a7cc13..044348d 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ This repo is a designed to be a Dockerized Rails 6 template: - [ActiveAdmin](https://activeadmin.info/) - [Devise](https://github.com/heartcombo/devise) +## Setup + + - `bundle` + - `yarn install --check-files` + - `Find/replace all TODO` + ### Dependencies This service has dependencies on the following services. @@ -40,7 +46,7 @@ To start the application using docker compose locally: | Variable | Description | Default | | ---------------|:---------------|:-----------------------:| -| `APP_NAME` | App name | TODO +| `APP_NAME` | App name | app | `APP_TITLE` | App title | TODO | `DATABASE_URL` | Database URL | postgres://localhost:5432/app_development | `DB_CONNECT_TIMEOUT` | Db connect timeout | 10 diff --git a/app/admin/dashboard.rb b/app/admin/dashboard.rb index c61d14d..9e535a8 100644 --- a/app/admin/dashboard.rb +++ b/app/admin/dashboard.rb @@ -1,11 +1,13 @@ -ActiveAdmin.register_page "Dashboard" do - menu priority: 1, label: proc { I18n.t("active_admin.dashboard") } +# frozen_string_literal: true - content title: proc { I18n.t("active_admin.dashboard") } do - div class: "blank_slate_container", id: "dashboard_default_message" do - span class: "blank_slate" do - span I18n.t("active_admin.dashboard_welcome.welcome") - small I18n.t("active_admin.dashboard_welcome.call_to_action") +ActiveAdmin.register_page 'Dashboard' do + menu priority: 1, label: proc { I18n.t('active_admin.dashboard') } + + content title: proc { I18n.t('active_admin.dashboard') } do + div class: 'blank_slate_container', id: 'dashboard_default_message' do + span class: 'blank_slate' do + span I18n.t('active_admin.dashboard_welcome.welcome') + small I18n.t('active_admin.dashboard_welcome.call_to_action') end end diff --git a/config/application.rb b/config/application.rb index c0578d2..93b1179 100644 --- a/config/application.rb +++ b/config/application.rb @@ -23,7 +23,7 @@ Bundler.require(*Rails.groups) module App module Config - APP_NAME = 'TODO: Update in config/application.rb' + APP_NAME = 'app' # TODO: Update name APP_TITLE = 'TODO: Update in config/application.rb' DATABASE_URL = ENV.fetch('DATABASE_URL') { "postgres://localhost:5432/#{APP_NAME}_#{Rails.env}" } REDIS_URL = ENV.fetch('REDIS_URL') { 'redis://localhost:6379/0' } diff --git a/db/migrate/20200319234126_create_active_admin_comments.rb b/db/migrate/20200319234126_create_active_admin_comments.rb index 0332a27..f854cad 100644 --- a/db/migrate/20200319234126_create_active_admin_comments.rb +++ b/db/migrate/20200319234126_create_active_admin_comments.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CreateActiveAdminComments < ActiveRecord::Migration[6.0] def self.up create_table :active_admin_comments do |t| diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 1dd86c7..d50eb0e 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -3,6 +3,6 @@ FactoryBot.define do factory :user do email { Faker::Internet.email } - password { Faker::Internet.password(6, 50) } + password { Faker::Internet.password } end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..a825cf9 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' + +require File.expand_path('../config/environment', __dir__) + +# Prevent database truncation if the environment is production +abort('The Rails environment is running in production mode!') if Rails.env.production? +require 'rspec/rails' + +# Add additional requires below this line. Rails is not loaded until this point! +Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + puts e.to_s.strip + exit 1 +end +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, :type => :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..cb426ff --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,101 @@ +# frozen_string_literal: true + +# 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. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration + +require 'simplecov' + +SimpleCov.start 'rails' + +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 + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + # config.disable_monkey_patching! + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = "doc" + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed +end diff --git a/spec/support/capybara_driver.rb b/spec/support/capybara_driver.rb new file mode 100644 index 0000000..9e6e813 --- /dev/null +++ b/spec/support/capybara_driver.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# require 'capybara/rspec' +# require 'selenium/webdriver' +# +# Capybara.register_driver :headless_chromium do |app| +# options = Selenium::WebDriver::Chrome::Options.new +# options.headless! +# options.add_argument '--window-size=1024,768' +# # https://blog.phusion.nl/2018/05/24/using-chrome-headless-selenium-and-capybara-inside-gitlab-runner-and-docker/ +# options.add_argument '--no-sandbox' +# +# Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) +# end diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 0000000..651a35e --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +# require 'database_cleaner' +# +# RSpec.configure do |config| +# config.use_transactional_fixtures = false +# +# config.before(:suite) do +# # https://github.com/DatabaseCleaner/database_cleaner#safeguards +# DatabaseCleaner.allow_remote_database_url = true +# +# if config.use_transactional_fixtures? +# raise(<<-MSG) +# Delete line `config.use_transactional_fixtures = true` from rails_helper.rb +# (or set it to false) to prevent uncommitted transactions being used in +# JavaScript-dependent specs. +# +# During testing, the app-under-test that the browser driver connects to +# uses a different database connection to the database connection used by +# the spec. The app's database connection would not be able to access +# uncommitted transaction data setup over the spec's database connection. +# MSG +# end +# +# DatabaseCleaner.clean_with(:truncation) +# end +# +# config.before(:each) do +# DatabaseCleaner.strategy = :transaction +# 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 +# +# unless 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.append_after(:each) do +# DatabaseCleaner.clean +# end +# end diff --git a/spec/support/shoulda_matchers.rb b/spec/support/shoulda_matchers.rb new file mode 100644 index 0000000..f6add5a --- /dev/null +++ b/spec/support/shoulda_matchers.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +require 'shoulda-matchers' + +Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :rails + end +end diff --git a/spec/support/validates_email_format.rb b/spec/support/validates_email_format.rb new file mode 100644 index 0000000..3cc3618 --- /dev/null +++ b/spec/support/validates_email_format.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +require 'validates_email_format_of/rspec_matcher' diff --git a/spec/support/wait_for_ajax.rb b/spec/support/wait_for_ajax.rb new file mode 100644 index 0000000..8ba015f --- /dev/null +++ b/spec/support/wait_for_ajax.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +# spec/support/wait_for_ajax.rb +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? + rescue StandardError + sleep 2 # anti-pattern, but no other solutions worked as reliably + true + end +end + +RSpec.configure do |config| + config.include WaitForAjax, type: :feature +end