Setup Rails Engine with RSpec, Capybara, and FactoryGirl

How to configure your Rails engine with Rspec, Capybara, and FactoryGirl

1. Run the follow lines to generate a mountable engine
1
rails plugin new ENGINE_NAME --dummy-path=spec/dummy --skip-test-unit --mountable

2. Add these lines to the gemspec file:
1
2
3
s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'capybara'
s.add_development_dependency 'factory_girl_rails'
3. Add this line to your gemspec file:
1
s.test_files = Dir["spec/**/*"]
4. Modify Rakefile to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path("../dummy/config/environment.rb", __FILE__)
require 'rspec/rails'
require 'factory_girl_rails'
Rails.backtrace_cleaner.remove_silencers!
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
end
5. Finally, add this config to your engine file (lives at lib/my_engine/engine.rb):
1
2
3
4
5
6
7
8
9
10
module MyEngine
class Engine < ::Rails::Engine
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
end
end


Here, we’re telling Rails when generating models, controllers, etc. for your engine to use RSpec and FactoryGirl, instead of the default of Test::Unit and fixtures.

After this, you can start writing specs with FactoryGirl factories and integration specs (inside the spec/features directory) with Capybara. Have fun testing!