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.
- 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 withgroup :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
- 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
- Prepare your
spec/rails_helper.rb
file (after this step the file should look like thisrails_helper.rb
file).- Uncomment requires for
spec/support
at L23;Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
- Setup RSpec and Capybara.
- 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'
- At the end of the
RSpec.configure
block add:config.include Capybara::RSpecMatchers, type: :input
- Below
- Setup database cleanup for Fedora/Solr and the relational database:
- Add
Database::Cleaner
andActiveFedora::Cleaner
at L11:
require 'active_fedora/cleaner' require 'database_cleaner'
- 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
- Add
- Setup user login in the test suite:
- 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! }
- At the end of the
- Uncomment requires for
- Prepare your
spec/spec_helper.rb
(after this step the file should look like thisspec_helper.rb
file).- Remove the
=begin
and=end
for the comment block at L49 and L95. - Re-comment L60
# config.example_status_persistence_file_path = "spec/examples.txt"
and# config.disable_monkey_patching!
- Remove the
- Add a
rake ci
task.- Copy this
ci.rake
file to thelib/tasks
folder.
- Copy this
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
- 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
- In a new terminal session, start the test servers and keep them running:
rails hydra:test_server
- In your main terminal session, run your test suite
rails spec
- 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 ourapp
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?