-
Notifications
You must be signed in to change notification settings - Fork 1
Run Test Suite
- Set up the application for RSpec
- Run the local test suite
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.
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-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
forgroup :development, :test do
; you'll need to combine these and eliminate any duplicates. -
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 }
-
- 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'
- At the end of the
RSpec.configure
block at L64 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
-
-
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"
- Re-comment L67
# 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
.
This is useful with continuous integration (CI) services like TravisCI
- Run the CI task. You should see a passing test suite:
rails ci
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/image_spec.rb:6
- 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 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?