Skip to content

Instantly share code, notes, and snippets.

@dminchev
Last active October 24, 2023 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminchev/27f8fd78a229b20881a9 to your computer and use it in GitHub Desktop.
Save dminchev/27f8fd78a229b20881a9 to your computer and use it in GitHub Desktop.
Init rails engine with rspec

Init the engine

rails plugin new engine_name --dummy-path=spec/dummy --skip-test-unit --mountable --database=postgresql

Add dependecies to the gemspec

s.add_development_dependency 'rspec-rails'
s.add_development_dependency 'capybara'
s.add_development_dependency 'factory_girl_rails'
s.add_development_dependency 'database_cleaner'
s.add_development_dependency 'faker'
s.add_development_dependency 'pry'

s.test_files = Dir["spec/**/*"]

Update Rakefile

#!/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

Create spec/spec_helper.rb

ENV['RAILS_ENV'] ||= 'test'

require File.expand_path("../dummy/config/environment.rb",  __FILE__)
require 'rspec/rails'
require 'factory_girl_rails'
require 'database_cleaner'
require 'faker'
require 'pry'

Rails.backtrace_cleaner.remove_silencers!
# Load support files

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

RSpec.configure do |config|
  config.color = true
  config.fail_fast = ENV['FAIL_FAST'] || false
  config.fixture_path = File.join(File.expand_path(File.dirname(__FILE__)), "fixtures")
  config.infer_spec_type_from_file_location!
  config.mock_with :rspec
  config.raise_errors_for_deprecations!

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, comment the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  config.before :each do
    Rails.cache.clear
  end

  config.include FactoryGirl::Syntax::Methods

  # Clean out the database state before the tests run
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.strategy = :transaction
  end

  # Wrap all db isolated tests in a transaction
  config.around(db: :isolate) do |example|
    DatabaseCleaner.cleaning(&example)
  end

  config.around do |example|
    Timeout.timeout(20, &example)
  end
end

Add config to the lib/<engine_name>/engine.rb

initializer "<engine_name>", before: :load_config_initializers do |app|
  config.paths["db/migrate"].expanded.each do |expanded_path|
    Rails.application.config.paths["db/migrate"] << expanded_path
  end
end

config.generators do |g|
  g.test_framework :rspec,
    :fixtures => true,
    :view_specs => false,
    :helper_specs => false,
    :routing_specs => false,
    :controller_specs => true,
    :request_specs => true

  g.fixture_replacement :factory_girl, :dir => "spec/factories"
end

Run

bundle install
rake db:create
rake db:migrate
rake db:migrate RAILS_ENV=test
rspec spec/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment