RSpec中の特定のtypeでmodule include

RSpec.configure do |config|
  ...
  config.include(ControllerHelpers, :type => :controller)
  ...
end

Rails tips: テストから共通機能を切り出すリファクタリング(翻訳)|TechRacho(テックラッチョ)〜エンジニアの「?」を「!」に〜|BPS株式会社

Rspec Shared example

RSpec 3.5 has been released!

RSpec.shared_context "DB support" do
  let(:db) { MyORM.database }
  # Wrap each example in a transaction...
  around do |ex|
    db.transaction(:rollback => :always, &ex)
  end
  # Interleave example begin/end messages in DB logs so it
  # is clear which SQL statements come from which examples.
  before do |ex|
    db.logger.info "Beginning example: #{ex.metadata[:full_description}"
  end
  after do |ex|
    db.logger.info "Ending example: #{ex.metadata[:full_description}"
  end
end

include_context

RSpec.describe MyModel do
  include_context "DB support"
end

config.include_context

RSpec.configure do |config|
  config.include_context "DB support", :db
end

when_first_matching_example_defined

RSpec.configure do |config|
  config.when_first_matching_example_defined(:db) do
    require "support/db"
  end
end

Filter type

Run only request(type: :request) specs.

$ bundle exec rspec -t type:request