Skip to content

Run Test Suite

Mark Bussey edited this page Sep 23, 2018 · 21 revisions

Goals

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

Lesson Setup

If you are completing the lessons in order during the workshop, your system should be all ready. If you're completing this lesson on it's own, please complete the same Lesson Setup as the previous lesson.

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-rails', "~> 3.5"
        gem 'shoulda-matchers'
      end

    NOTE: The hyrax and blacklight generators include some, but not all of these gems for you. There may be multiple sections in your Gemfile for group :development, :test do; you'll need to combine these and eliminate any duplicates.

  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! at L9 add:
      require 'rspec/matchers'
      require 'capybara/rspec'
      require 'capybara/rails'
      1. At the end of the RSpec.configure block at L64 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"
    3. Re-comment L67 # 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/image_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 are the advantages of having the CI task launch Solr and Fedora test instances vs. leaving them running?
  • 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?