module RSpec::Rails

Namespace for rspec-rails code.

Constants

Assertions

Constant aliased to either Minitest or TestUnit, depending on what is loaded.

ControllerAssertionDelegator

@private

DIRECTORY_MAPPINGS

Mappings used by `infer_spec_type_from_file_location!`.

@api private

RoutingAssertionDelegator

@private

TestUnitAssertionAdapter

@private

Public Class Methods

add_test_type_configurations(config) click to toggle source

Sets up the different example group modules for the different spec types

@api private

# File lib/rspec/rails/configuration.rb, line 43
def self.add_test_type_configurations(config)
  config.include RSpec::Rails::ControllerExampleGroup, :type => :controller
  config.include RSpec::Rails::HelperExampleGroup,     :type => :helper
  config.include RSpec::Rails::ModelExampleGroup,      :type => :model
  config.include RSpec::Rails::RequestExampleGroup,    :type => :request
  config.include RSpec::Rails::RoutingExampleGroup,    :type => :routing
  config.include RSpec::Rails::ViewExampleGroup,       :type => :view
  config.include RSpec::Rails::FeatureExampleGroup,    :type => :feature
  config.include RSpec::Rails::Matchers
  config.include RSpec::Rails::SystemExampleGroup, :type => :system
end
initialize_configuration(config) click to toggle source

@private rubocop:disable Style/MethodLength

# File lib/rspec/rails/configuration.rb, line 57
def self.initialize_configuration(config)
  config.backtrace_exclusion_patterns << /vendor\//
  config.backtrace_exclusion_patterns << %r{lib/rspec/rails}

  # controller settings
  config.add_setting :infer_base_class_for_anonymous_controllers, :default => true

  # fixture support
  config.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples
  config.add_setting :use_instantiated_fixtures
  config.add_setting :global_fixtures
  config.add_setting :fixture_path
  config.include RSpec::Rails::FixtureSupport, :use_fixtures

  # We'll need to create a deprecated module in order to properly report to
  # gems / projects which are relying on this being loaded globally.
  #
  # See rspec/rspec-rails#1355 for history
  #
  # @deprecated Include `RSpec::Rails::RailsExampleGroup` or
  #   `RSpec::Rails::FixtureSupport` directly instead
  config.include RSpec::Rails::FixtureSupport

  if ::Rails::VERSION::STRING > '5'
    config.add_setting :file_fixture_path, :default => 'spec/fixtures/files'
    config.include RSpec::Rails::FileFixtureSupport
  end

  # Add support for fixture_path on fixture_file_upload
  config.include RSpec::Rails::FixtureFileUploadSupport

  # This allows us to expose `render_views` as a config option even though it
  # breaks the convention of other options by using `render_views` as a
  # command (i.e. `render_views = true`), where it would normally be used
  # as a getter. This makes it easier for rspec-rails users because we use
  # `render_views` directly in example groups, so this aligns the two APIs,
  # but requires this workaround:
  config.add_setting :rendering_views, :default => false

  config.instance_exec do
    def render_views=(val)
      self.rendering_views = val
    end

    def render_views
      self.rendering_views = true
    end

    def render_views?
      rendering_views
    end

    def infer_spec_type_from_file_location!
      DIRECTORY_MAPPINGS.each do |type, dir_parts|
        escaped_path = Regexp.compile(dir_parts.join('[\\\/]') + '[\\\/]')
        define_derived_metadata(:file_path => escaped_path) do |metadata|
          metadata[:type] ||= type
        end
      end
    end

    # Adds exclusion filters for gems included with Rails
    def filter_rails_from_backtrace!
      filter_gems_from_backtrace "actionmailer", "actionpack", "actionview"
      filter_gems_from_backtrace "activemodel", "activerecord",
                                 "activesupport", "activejob"
    end
  end

  add_test_type_configurations(config)

  if defined?(::Rails::Controller::Testing)
    [:controller, :view, :request].each do |type|
      config.include ::Rails::Controller::Testing::TestProcess, :type => type
      config.include ::Rails::Controller::Testing::TemplateAssertions, :type => type
      config.include ::Rails::Controller::Testing::Integration, :type => type
    end
  end

  if defined?(ActionMailer)
    config.include RSpec::Rails::MailerExampleGroup, :type => :mailer
  end

  if defined?(ActiveJob)
    config.include RSpec::Rails::JobExampleGroup, :type => :job
  end
end

Private Class Methods

disable_testunit_autorun() click to toggle source

@private

# File lib/rspec/rails/adapters.rb, line 9
def self.disable_testunit_autorun
  # `Test::Unit::AutoRunner.need_auto_run=` was introduced to the test-unit
  # gem in version 2.4.9. Previous to this version `Test::Unit.run=` was
  # used. The implementation of test-unit included with Ruby has neither
  # method.
  if defined?(Test::Unit::AutoRunner.need_auto_run = ())
    Test::Unit::AutoRunner.need_auto_run = false
  elsif defined?(Test::Unit.run = ())
    Test::Unit.run = false
  end
end

Public Instance Methods

filter_rails_from_backtrace!() click to toggle source

Adds exclusion filters for gems included with Rails

# File lib/rspec/rails/configuration.rb, line 119
def filter_rails_from_backtrace!
  filter_gems_from_backtrace "actionmailer", "actionpack", "actionview"
  filter_gems_from_backtrace "activemodel", "activerecord",
                             "activesupport", "activejob"
end
infer_spec_type_from_file_location!() click to toggle source
# File lib/rspec/rails/configuration.rb, line 109
def infer_spec_type_from_file_location!
  DIRECTORY_MAPPINGS.each do |type, dir_parts|
    escaped_path = Regexp.compile(dir_parts.join('[\\\/]') + '[\\\/]')
    define_derived_metadata(:file_path => escaped_path) do |metadata|
      metadata[:type] ||= type
    end
  end
end
render_views() click to toggle source
# File lib/rspec/rails/configuration.rb, line 101
def render_views
  self.rendering_views = true
end
render_views=(val) click to toggle source
# File lib/rspec/rails/configuration.rb, line 97
def render_views=(val)
  self.rendering_views = val
end
render_views?() click to toggle source
# File lib/rspec/rails/configuration.rb, line 105
def render_views?
  rendering_views
end