Skip to content

Instantly share code, notes, and snippets.

@BideoWego
Last active January 13, 2022 12:20
Show Gist options
  • Save BideoWego/973df3cf566b99513da8 to your computer and use it in GitHub Desktop.
Save BideoWego/973df3cf566b99513da8 to your computer and use it in GitHub Desktop.

Revisions

  1. BideoWego revised this gist May 2, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions a_rails_rspec_checklist\README.md
    Original file line number Diff line number Diff line change
    @@ -13,9 +13,9 @@
    - `guard-rspec`
    - `factory_bot_rails`
    - `database_cleaner`
    - `capybara`
    - `capybara` (Included with Rails 6)
    - `launchy`
    - `selenium-webdriver`
    - `selenium-webdriver` (Included with Rails 6)

    - Run the necessary initializers with bundle, guard, and rspec
    - `$ bundle exec guard init`
  2. BideoWego revised this gist May 2, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion a_rails_rspec_checklist\README.md
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    - `rspec-rails`
    - `guard`
    - `guard-rspec`
    - `factory_girl_rails`
    - `factory_bot_rails`
    - `database_cleaner`
    - `capybara`
    - `launchy`
  3. BideoWego renamed this gist Apr 17, 2018. 1 changed file with 0 additions and 0 deletions.
  4. BideoWego renamed this gist Apr 17, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. BideoWego renamed this gist Apr 17, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. BideoWego renamed this gist Apr 8, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. BideoWego revised this gist Mar 26, 2016. 1 changed file with 67 additions and 12 deletions.
    79 changes: 67 additions & 12 deletions rails_helper.rb
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,84 @@
    # This file is copied to spec/ when you run 'rails generate rspec:install'
    ENV['RAILS_ENV'] ||= 'test'
    require File.expand_path('../../config/environment', __FILE__)
    # Prevent database truncation if the environment is production
    abort("The Rails environment is running in production mode!") if Rails.env.production?
    require 'spec_helper'
    require 'rspec/rails'
    # Add additional requires below this line. Rails is not loaded until this point!

    # ------------------------------------
    # My Requires
    # ------------------------------------
    require 'capybara/rails'
    require 'factory_girl_rails'
    require 'database_cleaner'

    # Uncomment this line
    # 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 migration and applies them before tests are run.
    # If you are not using ActiveRecord, you can remove this line.
    ActiveRecord::Migration.maintain_test_schema!

    RSpec.configure do |config|

    # ------------------------------------
    # My Config
    # ------------------------------------
    config.include FactoryGirl::Syntax::Methods

    config.include Macros::Global
    config.include Macros::Flash
    config.include Macros::User
    config.include Macros::Secret

    config.include ControllerHelper

    # config.fixture_path = "#{::Rails.root}/spec/fixtures" #<<<< comment out
    config.use_transactional_fixtures = false #<<<<< should be false when using FactoryGirl

    config.include Macros::Post
    config.include Macros::Session
    config.include Macros::Search
    config.include Macros::User

    end
    config.include ControllerHelper

    config.after(:all) do
    if Rails.env.test?
    FileUtils.rm_rf(Dir["#{Rails.root}/public/system/test"])
    end
    end


    # 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 = false

    # 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
  8. BideoWego revised this gist Mar 26, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions checklist.md
    Original file line number Diff line number Diff line change
    @@ -45,8 +45,8 @@

    - See [rails_helper.rb](https://gist.github.com/BideoWego/973df3cf566b99513da8) for relevant changes to rails_helper.rb
    - Under the line that says
    - \# Add additional requires below this line. Rails is not loaded until this point!
    - Require capybara
    - `# Add additional requires below this line. Rails is not loaded until this point!`
    - Require `capybara`
    - Require the `spec/support` folder recursively in `rails_helper.rb`
    - Inside the config block
    - Include FactoryGirl methods into RSpec
  9. BideoWego revised this gist Mar 26, 2016. 1 changed file with 42 additions and 39 deletions.
    81 changes: 42 additions & 39 deletions checklist.md
    Original file line number Diff line number Diff line change
    @@ -7,82 +7,85 @@

    - Add the following gems to Gemfile in a development, test group

    - hirb
    - rspec-rails
    - guard
    - guard-rspec
    - factory_girl_rails
    - database_cleaner
    - capybara
    - launchy
    - selenium-webdriver
    - `hirb`
    - `rspec-rails`
    - `guard`
    - `guard-rspec`
    - `factory_girl_rails`
    - `database_cleaner`
    - `capybara`
    - `launchy`
    - `selenium-webdriver`

    - Run the necessary initializers with bundle, guard, and rspec
    - `$ bundle exec guard init`
    - `$ rails g rspec:install`

    - In the Guardfile comment out the following lines
    - watch(rspec.spec_helper) { rspec.spec_dir }
    - `watch(rspec.spec_helper) { rspec.spec_dir }`
    - Ignore when spec_helper is saved
    - watch(rspec.spec_support) { rspec.spec_dir }
    - `watch(rspec.spec_support) { rspec.spec_dir }`
    - Ignore when a support file is saved

    - Run the necessary initializers with bundle, guard, and rspec
    - guard init
    - rails g rspec:install

    - Prepare test database with $ rake db:test:prepare
    - Prepare test database with `$ rake db:test:prepare`

    - Ensure you have a spec/ folder in root directory
    - Ensure you have a `spec/` folder in root directory

    - Ensure you have a spec_helper.rb and rails_helper.rb in the spec/ folder
    - Ensure you have a `spec_helper.rb` and `rails_helper.rb` in the `spec/` folder

    - Create a spec/factories/ folder
    - Add any model factories in model_factory.rb files
    - Create a `spec/factories/` folder
    - Add any model factories in `model_factory.rb` files
    - Be sure to use sequences for factories that will have multiple instances
    - Be sure to include associations

    - Create a spec/support/ folder
    - Create a `spec/support/` folder
    - Add [database_cleaner.rb](https://gist.github.com/BideoWego/973df3cf566b99513da8) to support for database_cleaner

    - Create a spec/support/macros folder
    - Create a `spec/support/macros` folder

    - See [rails_helper.rb](https://gist.github.com/BideoWego/973df3cf566b99513da8) for relevant changes to rails_helper.rb
    - Under the line that says
    - \# Add additional requires below this line. Rails is not loaded until this point!
    - Require capybara
    - Require the spec/support folder recursively in rails_helper.rb
    - Require the `spec/support` folder recursively in `rails_helper.rb`
    - Inside the config block
    - Include FactoryGirl methods into RSpec
    - Disable fixtures

    - Any helpers for Capybara should go in a appropriately named spec/support/marcos file and be name spaced under a module called Macros
    - See the example [here](https://gist.github.com/BideoWego/973df3cf566b99513da8) in global.rb
    - Any helpers for Capybara should go in a appropriately named `spec/support/marcos` file and be name spaced under a module called Macros
    - See the example [here](https://gist.github.com/BideoWego/973df3cf566b99513da8) in `global.rb`

    - A spec/support/controller_helper.rb and spec/support/view_helper.rb can be used for shared controller and view helper methods
    - See controller_helper.rb and view_helper.rb here
    - A `spec/support/controller_helper.rb` and `spec/support/view_helper.rb` can be used for shared controller and view helper methods
    - See `controller_helper.rb` and `view_helper.rb` here

    - Capybara macros and other helper modules from spec/support should be included like
    - Capybara macros and other helper modules from `spec/support` should be included like

    - FactoryGirl in the rails_helper when they are to be available globally
    - FactoryGirl in the `rails_helper` when they are to be available globally

    - For the view_helper.rb module it should be used as shown in the index_spec.rb https://gist.github.com/BideoWego/973df3cf566b99513da8 where the view object extends the module
    - For the `view_helper.rb` module it should be used as shown in the `index_spec.rb` https://gist.github.com/BideoWego/973df3cf566b99513da8 where the view object extends the module
    - This includes the module methods on the view object in the current spec and allows it to access the methods as expected in normal rails application flow

    - Create the following folders
    - spec/controllers/
    - spec/features/
    - spec/models/
    - spec/views/
    - `spec/controllers/`
    - `spec/features/`
    - `spec/models/`
    - `spec/views/`

    - Any spec that requires javascript should use the format shown in [secret_spec.rb](https://gist.github.com/BideoWego/973df3cf566b99513da8)
    - This will use the selenium-webdriver with capybara to launch FireFox and perform the spec live
    - NOTE the use of page.driver.browser.switch_to.alert.accept to accept the alert box
    - This will use the `selenium-webdriver` with `capybara` to launch FireFox and perform the spec live
    - NOTE the use of `page.driver.browser.switch_to.alert.accept` to accept the alert box

    - As a final note, be aware than if you ever run ALL tests with the rspec command, FactoryGirl will increment sequences from the first test all the way to the last test.
    - This means it is possible for the number value to effect tests differently based on where they fall into the overall test suite
    - For example: if there is a validation on length of a string and the test is run after many many tests, the incremented value might be 1000 instead of 10
    - For example: if there is a validation on length of a string and the test is run after many many tests, the incremented value might be `1000` instead of `10`
    - This could cause unexpected validation errors because the extra few chars pushed the otherwise valid string over the maximum
    - JUST BE AWARE OF THIS!!!!

    - While testing use $ tail -f log/test.log to view the server output from tests
    Done! Happy testing!
    - While testing use `$ tail -f log/test.log` to view the server output from tests

    ### Done!

    ## Happy testing!



  10. BideoWego revised this gist Jan 26, 2016. No changes.
  11. BideoWego revised this gist Jan 26, 2016. 1 changed file with 89 additions and 0 deletions.
    89 changes: 89 additions & 0 deletions checklist.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    # Rails RSpec Checklist

    - Ensure turbolinks is disabled for good measure
    - comment out gem
    - remove from javascript asset pipeline
    - remove from application layout

    - Add the following gems to Gemfile in a development, test group

    - hirb
    - rspec-rails
    - guard
    - guard-rspec
    - factory_girl_rails
    - database_cleaner
    - capybara
    - launchy
    - selenium-webdriver

    - In the Guardfile comment out the following lines
    - watch(rspec.spec_helper) { rspec.spec_dir }
    - Ignore when spec_helper is saved
    - watch(rspec.spec_support) { rspec.spec_dir }
    - Ignore when a support file is saved

    - Run the necessary initializers with bundle, guard, and rspec
    - guard init
    - rails g rspec:install

    - Prepare test database with $ rake db:test:prepare

    - Ensure you have a spec/ folder in root directory

    - Ensure you have a spec_helper.rb and rails_helper.rb in the spec/ folder

    - Create a spec/factories/ folder
    - Add any model factories in model_factory.rb files
    - Be sure to use sequences for factories that will have multiple instances
    - Be sure to include associations

    - Create a spec/support/ folder
    - Add [database_cleaner.rb](https://gist.github.com/BideoWego/973df3cf566b99513da8) to support for database_cleaner

    - Create a spec/support/macros folder

    - See [rails_helper.rb](https://gist.github.com/BideoWego/973df3cf566b99513da8) for relevant changes to rails_helper.rb
    - Under the line that says
    - \# Add additional requires below this line. Rails is not loaded until this point!
    - Require capybara
    - Require the spec/support folder recursively in rails_helper.rb
    - Inside the config block
    - Include FactoryGirl methods into RSpec
    - Disable fixtures

    - Any helpers for Capybara should go in a appropriately named spec/support/marcos file and be name spaced under a module called Macros
    - See the example [here](https://gist.github.com/BideoWego/973df3cf566b99513da8) in global.rb

    - A spec/support/controller_helper.rb and spec/support/view_helper.rb can be used for shared controller and view helper methods
    - See controller_helper.rb and view_helper.rb here

    - Capybara macros and other helper modules from spec/support should be included like

    - FactoryGirl in the rails_helper when they are to be available globally

    - For the view_helper.rb module it should be used as shown in the index_spec.rb https://gist.github.com/BideoWego/973df3cf566b99513da8 where the view object extends the module
    - This includes the module methods on the view object in the current spec and allows it to access the methods as expected in normal rails application flow

    - Create the following folders
    - spec/controllers/
    - spec/features/
    - spec/models/
    - spec/views/

    - Any spec that requires javascript should use the format shown in [secret_spec.rb](https://gist.github.com/BideoWego/973df3cf566b99513da8)
    - This will use the selenium-webdriver with capybara to launch FireFox and perform the spec live
    - NOTE the use of page.driver.browser.switch_to.alert.accept to accept the alert box

    - As a final note, be aware than if you ever run ALL tests with the rspec command, FactoryGirl will increment sequences from the first test all the way to the last test.
    - This means it is possible for the number value to effect tests differently based on where they fall into the overall test suite
    - For example: if there is a validation on length of a string and the test is run after many many tests, the incremented value might be 1000 instead of 10
    - This could cause unexpected validation errors because the extra few chars pushed the otherwise valid string over the maximum
    - JUST BE AWARE OF THIS!!!!

    - While testing use $ tail -f log/test.log to view the server output from tests
    Done! Happy testing!




  12. BideoWego revised this gist Nov 22, 2015. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions rails_helper.rb
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,8 @@
    # ------------------------------------
    require 'capybara/rails'

    Dir[
    Rails.root.join("spec/support/**/*.rb")
    ].each { |f| require f }
    # Uncomment this line
    Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

    RSpec.configure do |config|

  13. BideoWego revised this gist Nov 22, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions rails_helper.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Add additional requires below this line. Rails is not loaded until this point!

    # ------------------------------------
    # My Requires
    # ------------------------------------
  14. BideoWego revised this gist Nov 22, 2015. 1 changed file with 44 additions and 0 deletions.
    44 changes: 44 additions & 0 deletions secrets_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    # spec/features/secret_spec.rb

    require 'rails_helper'

    feature 'Secrets' do
    let!(:user){create(:user)}
    let!(:secrets){create_list(:secret, 5, :author => user)}
    let!(:user_one){create(:user, :name => 'User One', :email => '[email protected]')}
    let!(:user_two){create(:user, :name => 'User Two', :email => '[email protected]')}
    let!(:secrets_user_one){create_list(:secret, 5, :author => user_one)}
    let!(:secrets_user_two){create_list(:secret, 5, :author => user_two)}

    # ----------------------------------------
    # Delete Secret
    # ----------------------------------------
    feature 'deletion' do
    context 'when the user is logged in' do
    before do
    visit new_session_path
    sign_in(user)
    visit secrets_path
    end

    after do
    visit logout_path
    end

    scenario 'is enabled on current user secrets', :js => true do
    link = find('a', :text => 'Destroy', :match => :first)
    link.click
    page.driver.browser.switch_to.alert.accept
    expect(page).to have_content(success_text_for('Secret', 'destroy'))
    end

    scenario 'redirects to secrets listing upon deletion', :js => true do
    link = find('a', :text => 'Destroy', :match => :first)
    link.click
    page.driver.browser.switch_to.alert.accept
    expect(page).to have_content('Listing secrets')
    end
    end

    end
    end
  15. BideoWego revised this gist Nov 22, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions rails_helper.rb
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,12 @@
    # ------------------------------------
    config.include FactoryGirl::Syntax::Methods

    config.include Macros::Global
    config.include Macros::Flash
    config.include Macros::User
    config.include Macros::Secret

    config.include ControllerHelper

    # config.fixture_path = "#{::Rails.root}/spec/fixtures" #<<<< comment out
    config.use_transactional_fixtures = false #<<<<< should be false when using FactoryGirl
  16. BideoWego revised this gist Nov 22, 2015. 3 changed files with 54 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions controller_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    # spec/support/controller_helper.rb

    module ControllerHelper
    def login(user)
    session[:user_id] = user.id
    end

    def logout
    session.delete(:user_id)
    end

    def current_user
    User.find(session[:user_id]) if User.exists?(session[:user_id]) && session[:user_id]
    end

    def signed_in_user?
    !!current_user
    end
    end
    30 changes: 30 additions & 0 deletions index_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    # spec/views/index_spec.rb

    require 'rails_helper'

    describe 'secrets/index.html.erb' do
    let(:user){create(:user)}
    let(:users){[user, create(:user)]}
    let(:secrets){[create(:secret, :author => user), create(:secret)]}

    before do
    view.extend(ViewHelper)
    assign(:users, users)
    assign(:secrets, secrets)
    end

    context 'the user is logged in' do
    it 'shows the author of all secrets' do
    login(user)
    render
    expect(rendered).to_not have_content(hidden_text)
    end
    end

    context 'the user is logged out' do
    it 'does not show any author of any secrets' do
    render
    expect(rendered).to have_content(hidden_text)
    end
    end
    end
    5 changes: 5 additions & 0 deletions view_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # spec/support/view_helper.rb

    module ViewHelper
    include ControllerHelper
    end
  17. BideoWego revised this gist Nov 22, 2015. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions global.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # spec/support/macros/global.rb

    module Macros
    module Global
    def submit_form
    find('input[name="commit"]').click
    end
    end
    end
  18. BideoWego renamed this gist Nov 22, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.txt → rails_helper.rb
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,8 @@
    config.include FactoryGirl::Syntax::Methods


    # config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = false
    # config.fixture_path = "#{::Rails.root}/spec/fixtures" #<<<< comment out
    config.use_transactional_fixtures = false #<<<<< should be false when using FactoryGirl


    end
  19. BideoWego revised this gist Nov 22, 2015. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # ------------------------------------
    # My Requires
    # ------------------------------------
    require 'capybara/rails'

    Dir[
    Rails.root.join("spec/support/**/*.rb")
    ].each { |f| require f }

    RSpec.configure do |config|

    # ------------------------------------
    # My Config
    # ------------------------------------
    config.include FactoryGirl::Syntax::Methods


    # config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = false


    end
  20. BideoWego revised this gist Nov 22, 2015. No changes.
  21. BideoWego revised this gist Nov 22, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions database_cleaner.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # spec/support/database_cleaner.rb

    # From blog post
    # http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

  22. BideoWego created this gist Nov 22, 2015.
    26 changes: 26 additions & 0 deletions database_cleaner.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    # From blog post
    # http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/

    RSpec.configure do |config|

    config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    end

    config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    end

    config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
    end

    config.before(:each) do
    DatabaseCleaner.start
    end

    config.after(:each) do
    DatabaseCleaner.clean
    end

    end