Setup RSpec and Capybara on a new Rails engine
-
rails plugin new ENGINE_NAME --full --database=mysql
-
Add to
gemspec
:
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'shoulda-matchers'
s.add_development_dependency 'capybara'
s.add_development_dependency 'database_cleaner'
s.add_development_dependency 'factory_girl_rails', '~> 1.7'
-
Modify
gemspec
test_files line to this:s.test_files = Dir["spec/**/*"]
-
Modify
Rakefile
to look like this:
#!/usr/bin/env rake
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'
Bundler::GemHelper.install_tasks
Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
require 'rspec/core'
require 'rspec/core/rake_task'
desc "Run all specs in spec directory (excluding plugin specs)"
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
task :default => :spec
-
mkdir spec
-
mv test/dummy spec/
-
rm -fR test
-
Edit
spec/spec_helper.rb
:
$LOAD_PATH.unshift(File.dirname(__FILE__))
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'shoulda-matchers'
require 'capybara/rails'
require 'capybara/rspec'
require 'database_cleaner'
require 'factory_girl_rails'
Rails.backtrace_cleaner.remove_silencers!
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
DatabaseCleaner.strategy = :truncation
RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.after(:each, :type => :request) do
DatabaseCleaner.clean # Truncate the database
Capybara.reset_sessions! # Forget the (simulated) browser state
Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
end
end
- Add this to your engine file:
config.generators do |g|
g.test_framework :rspec, :fixture => false
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.assets false
g.helper false
end