Following are steps to initialize and configure a Rails 4 mountable engine. This guide uses MongoDB as its backing store and includes instructions for rspec configuration.
Change directory to the main applications root and run the following command:
$ rails plugin new components/<engine_name> --full --mountable --skip-active-record --dummy-path=spec/dummy --skip-test-unit
Now, edit file: components/<engine_name>/<engine_name>.gemspec :
Update/set:
- Metadata section
- test_files path, and
- gem dependencies for application and development:
s.test_files = Dir["spec/**/*"]
s.add_dependency "rails", "~> 4.2.7.1"
s.add_dependency "mongoid", "~> 5.4.0"
s.add_dependency "slim", "~> 3.0.8"
s.add_dependency 'pundit', '~> 1.0.1'
s.add_dependency 'active_model_serializers'
s.add_dependency 'language_list', '~> 1.1.0'
...
s.add_development_dependency "rspec-rails"
s.add_development_dependency "mongoid-rspec"
s.add_development_dependency 'shoulda-matchers'
s.add_development_dependency 'database_cleaner'
s.add_development_dependency 'capybara'
s.add_development_dependency 'factory_girl_rails'
s.add_development_dependency 'pry'
s.add_development_dependency 'pry-rails'
s.add_development_dependency 'pry-stack_explorer'
s.add_development_dependency 'pry-byebug'
s.add_development_dependency 'pry-remote'
s.add_development_dependency 'forgery'
Note: any gems in development status will fail if referenced in the above gemspec file. Reference development gems in the file: components/<engine_name>/Gemfile
In main application's Gemfile, add the following:
gem "<engine_name>", path: "components/<engine_name>"
Run the bundle command:
$ bundle install
In the main application's config/routes file, add the following:
mount <engine_name>::Engine, at: "/<engine_name>"
Edit file: components/<engine_name>/lib/<engine_name>/engine.rb
require "mongoid"
require “slim”
require 'pundit'
require 'active_model_serializers'
require 'language_list'
module <engine_name>
class Engine < ::Rails::Engine
isolate_namespace <engine_name>
config.generators do |g|
g.orm :mongoid
g.template_engine :slim
g.test_framework :rspec, :fixture => false
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.assets false
g.helper true
end
end
end
$ cd components/<engine_name>
$ bundle install
Run the rspec generation script
$ cd components/<engine_name>
$ rails g rspec:install
The initialization script will create configuration files. Add dummy spec path to the components/<engine_name>/spec/spec_helper.rb file:
...
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require File.expand_path("../dummy/config/environment", __FILE__)
...
In the same file, make sure the following lines are configured as followed and NOT commented out:
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
mocks.verify_partial_doubles = true
config.shared_context_metadata_behavior = :apply_to_host_groups
config.filter_run_when_matching :focus
config.disable_monkey_patching!
config.profile_examples = 10
config.order = :random
Add dependencies to the rails helper file components/<engine_name>/spec/rails_helper.rb
# 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 'mongoid-rspec'
require 'shoulda/matchers'
require 'database_cleaner'
require 'capybara/rails'
require 'capybara/rspec'
require "forgery"
require 'factory_girl_rails'
require 'test_prof'
require 'test_prof/recipes/rspec/factory_default'
require "pry"
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
end
end
In the same file, comment out the line (if present):
# ActiveRecord::Migration.maintain_test_schema!
Also in the same file, under RSpec.configure do |config| block:
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
...
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
# config.include ModelMatcherHelpers, :type => :model
config.include Mongoid::Matchers, type: :model
config.include Devise::TestHelpers, :type => :controller
config.include Devise::TestHelpers, :type => :view
config.include FactoryGirl::Syntax::Methods
config.include Capybara::DSL
config.after(:example, :dbclean => :after_each) do
DatabaseCleaner.clean
end
config.around(:example, :dbclean => :around_each) do |example|
DatabaseCleaner.clean
example.run
DatabaseCleaner.clean
end
end
$ cd components/<engine_name>/spec/dummy
$ rails g mongoid:config
Update the Rakefile to link the one in spec/dummy: (See "Rails Engine Testing with RSpec, Capybara, and FactoryGirl":https://www.viget.com/articles/rails-engine-testing-with-rspec-capybara-and-factorygirl)
Note: you will need to change directory to spec/dummy folder to invoke a rails console in context with the Rails engine package.