Run Test Suite - RepoCamp/ohsu2018 GitHub Wiki

Goals

  • Set up the application for RSpec
  • Run the local test suite

Set up RSpec

In this workshop, we're going to follow the practices recommended by Test Driven Development. That means we're going to be writing automated tests for each new feature we develop. In order to do that, we first need to set up an automated test suite.

The Samvera community tends to use RSpec for testing rails applications. When we generated a new Hyrax work type, we also auto-generated some RSpec tests for that new work type. However, we need to do some setup before those tests will run.

  1. Ensure the RSpec-required gems are included in your Gemfile. The file is found in the root of your Rails application. Look for a block beginning with group :development, :test do. The order of the gems isn't important and you may see other gems listed too.
      group :development, :test do
        # Call 'byebug' anywhere in the code to stop execution and get a debugger console
        gem 'byebug', platform: :mri
        gem 'capybara', '~> 2.13'
        gem 'database_cleaner'
        gem 'rspec', "~> 3.5"
        gem 'rspec-rails', "~> 3.5"
        gem 'shoulda-matchers'
      end
    
  2. Run bundler to make sure those gems are installed. It's good practice to run this anytime you make changes to your Gemfile:
    bundle install
  3. Prepare your spec/rails_helper.rb file (after this step the file should look like this rails_helper.rb file).
    1. Uncomment requires for spec/support at L23; Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
    2. Setup RSpec and Capybara.
      1. Below # Add additional requires below this line. Rails is not loaded until this point! (L9) add:
      require 'rspec/matchers'
      require 'capybara/rspec'
      require 'capybara/rails'
      
      1. At the end of the RSpec.configure block add: config.include Capybara::RSpecMatchers, type: :input
    3. Setup database cleanup for Fedora/Solr and the relational database:
      1. Add Database::Cleaner and ActiveFedora::Cleaner at L11:
      require 'active_fedora/cleaner'
      require 'database_cleaner'
      
      1. Setup cleaning as before/after example hooks; at the end of the RSpec.configure block add:
      config.before :suite do
        ActiveFedora::Cleaner.clean!
      end
      
      config.before :each do
        DatabaseCleaner.strategy = :truncation
        DatabaseCleaner.start
      end
      
      config.after do
        DatabaseCleaner.clean
      end
      
    4. Setup user login in the test suite:
      1. At the end of the RSpec.configure block add:
      config.include Devise::Test::ControllerHelpers, type: :controller
      config.include Warden::Test::Helpers, type: :feature
      config.after(:each, type: :feature) { Warden.test_reset! }
      
  4. Prepare your spec/spec_helper.rb (after this step the file should look like this spec_helper.rb file).
    1. Remove the =begin and =end for the comment block at L49 and L95.
    2. Re-comment L60 # config.example_status_persistence_file_path = "spec/examples.txt" and # config.disable_monkey_patching!
  5. Add a rake ci task.
    1. Copy this ci.rake file to the lib/tasks folder.

Note: You can see everything we just changed, compared to our base application, here. You can check out a version of our test application with a working test suite using
git checkout ci_setup.

Run the test suite

Option One: start servers as needed

This is useful with continuous integration (CI) services like TravisCI

  1. Run the CI task. You should see a passing test suite:
    rails ci

Option Two: keep test servers running in the background

This is useful if you want to run single tests, or run tests often and don't want the overhead of starting Solr and Fedora each time

  1. In a new terminal session, start the test servers and keep them running:
    rails hydra:test_server
  2. In your main terminal session, run your test suite
    rails spec
  3. You can run a single test like this
    rails spec SPEC=./spec/models/work_spec.rb:6

For discussion

  • Compare the structure of our /spec directory to our app directory
  • What is a rake task? What kinds of jobs make good tasks?
  • Let's read through the ci.rake file together. What is it doing?
  • CI stands for "continuous integration". What does that mean? How might we run this automatically? (e.g., travis-ci)
  • What does "pending" mean in the context of our test suite?
  • What does that Randomized with seed line mean? What are we randomizing and why?