Skip to content

Commit

Permalink
Set the application up to run an RSpec test suite
Browse files Browse the repository at this point in the history
The application generators add much of the code we need to run
RSpec, but we need to make a few tweaks to make writing and running
new tests easier.

We also add a `ci` rake task to let us easily start solr & fedora
and run the whole test suite.
  • Loading branch information
mark-dce committed Apr 22, 2018
1 parent 32245ab commit 2cd3fe4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 19 deletions.
35 changes: 35 additions & 0 deletions lib/tasks/ci.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
unless Rails.env.production?
APP_ROOT = File.dirname(__FILE__)
require "solr_wrapper"
require "fcrepo_wrapper"
require 'solr_wrapper/rake_task'

desc "Run Continuous Integration"
task :ci do
ENV["environment"] = "test"
solr_params = {
port: 8985,
verbose: true,
managed: true
}
fcrepo_params = {
port: 8986,
verbose: true,
managed: true,
enable_jms: false,
fcrepo_home_dir: 'tmp/fcrepo4-test-data'
}
SolrWrapper.wrap(solr_params) do |solr|
solr.with_collection(
name: "hydra-test",
persist: false,
dir: Rails.root.join("solr", "config")
) do
FcrepoWrapper.wrap(fcrepo_params) do
Rake::Task["spec"].invoke
end
end
end
# Rake::Task["doc"].invoke
end
end
56 changes: 52 additions & 4 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
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 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'spec_helper'
require 'action_view'
require 'devise'
require 'devise/version'
require 'rspec/matchers'
require 'active_fedora/cleaner'
require 'capybara/rspec'
require 'capybara/rails'
require 'database_cleaner'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
Expand All @@ -19,13 +27,20 @@
# 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 }
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

# See https://github.com/thoughtbot/shoulda-matchers#rspec
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
Expand Down Expand Up @@ -54,4 +69,37 @@
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

config.before :suite do
ActiveFedora::Cleaner.clean!
end

config.before :each do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
end

config.after do
DatabaseCleaner.clean
end

config.include Devise::Test::ControllerHelpers, type: :controller
config.include Capybara::RSpecMatchers, type: :input

config.include Warden::Test::Helpers, type: :feature
config.after(:each, type: :feature) { Warden.test_reset! }

# Gets around a bug in RSpec where helper methods that are defined in views aren't
# getting scoped correctly and RSpec returns "does not implement" errors. So we
# can disable verify_partial_doubles if a particular test is giving us problems.
# Ex:
# describe "problem test", verify_partial_doubles: false do
# ...
# end
config.before :each do |example|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = example.metadata.fetch(:verify_partial_doubles, true)
end
end

end
33 changes: 18 additions & 15 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# This file was generated by the `rails generate rspec:install` command.
# Conventionally, all specs live under a `spec` directory, which RSpec adds to
# the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
Expand All @@ -12,6 +13,9 @@
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
Expand Down Expand Up @@ -44,9 +48,9 @@
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.

# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
Expand All @@ -57,29 +61,29 @@
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# config.example_status_persistence_file_path = "spec/examples.txt"

# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# config.disable_monkey_patching!

# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
# config.default_formatter = 'doc'
# end

# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# config.profile_examples = 10

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
Expand All @@ -92,5 +96,4 @@
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end

0 comments on commit 2cd3fe4

Please sign in to comment.