123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Rails

Relationships & Source Files
Namespace Children
Modules:
Classes:
Defined in: rspec-rails/lib/rspec-rails.rb,
rspec-rails/lib/rspec/rails/active_record.rb,
rspec-rails/lib/rspec/rails/adapters.rb,
rspec-rails/lib/rspec/rails/configuration.rb,
rspec-rails/lib/rspec/rails/feature_check.rb,
rspec-rails/lib/rspec/rails/file_fixture_support.rb,
rspec-rails/lib/rspec/rails/fixture_file_upload_support.rb,
rspec-rails/lib/rspec/rails/fixture_support.rb,
rspec-rails/lib/rspec/rails/matchers.rb,
rspec-rails/lib/rspec/rails/version.rb,
rspec-rails/lib/rspec/rails/view_assigns.rb,
rspec-rails/lib/rspec/rails/view_path_builder.rb,
rspec-rails/lib/rspec/rails/view_rendering.rb,
rspec-rails/lib/rspec/rails/view_spec_methods.rb,
rspec-rails/lib/rspec/rails/example/channel_example_group.rb,
rspec-rails/lib/rspec/rails/example/channel_example_group.rb,
rspec-rails/lib/rspec/rails/example/controller_example_group.rb,
rspec-rails/lib/rspec/rails/example/feature_example_group.rb,
rspec-rails/lib/rspec/rails/example/helper_example_group.rb,
rspec-rails/lib/rspec/rails/example/job_example_group.rb,
rspec-rails/lib/rspec/rails/example/job_example_group.rb,
rspec-rails/lib/rspec/rails/example/mailbox_example_group.rb,
rspec-rails/lib/rspec/rails/example/mailer_example_group.rb,
rspec-rails/lib/rspec/rails/example/mailer_example_group.rb,
rspec-rails/lib/rspec/rails/example/model_example_group.rb,
rspec-rails/lib/rspec/rails/example/rails_example_group.rb,
rspec-rails/lib/rspec/rails/example/request_example_group.rb,
rspec-rails/lib/rspec/rails/example/routing_example_group.rb,
rspec-rails/lib/rspec/rails/example/system_example_group.rb,
rspec-rails/lib/rspec/rails/example/view_example_group.rb,
rspec-rails/lib/rspec/rails/matchers/action_cable.rb,
rspec-rails/lib/rspec/rails/matchers/action_mailbox.rb,
rspec-rails/lib/rspec/rails/matchers/active_job.rb,
rspec-rails/lib/rspec/rails/matchers/base_matcher.rb,
rspec-rails/lib/rspec/rails/matchers/be_a_new.rb,
rspec-rails/lib/rspec/rails/matchers/be_new_record.rb,
rspec-rails/lib/rspec/rails/matchers/be_valid.rb,
rspec-rails/lib/rspec/rails/matchers/have_enqueued_mail.rb,
rspec-rails/lib/rspec/rails/matchers/have_http_status.rb,
rspec-rails/lib/rspec/rails/matchers/have_rendered.rb,
rspec-rails/lib/rspec/rails/matchers/redirect_to.rb,
rspec-rails/lib/rspec/rails/matchers/routing_matchers.rb,
rspec-rails/lib/rspec/rails/matchers/send_email.rb,
rspec-rails/lib/rspec/rails/matchers/action_cable/have_broadcasted_to.rb,
rspec-rails/lib/rspec/rails/matchers/action_cable/have_streams.rb

Overview

Namespace for rspec-rails code.

Constant Summary

Class Method Summary

Instance Method Summary

Class Method Details

.add_test_type_configurations(config)

This method is for internal use only.

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

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/configuration.rb', line 47

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

.disable_testunit_autorun (private)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-rails/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

.initialize_configuration(config)

This method is for internal use only.
[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/configuration.rb', line 60

def self.initialize_configuration(config) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity
  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_active_record, default: true
  config.add_setting :use_transactional_fixtures, alias_with: :use_transactional_examples
  config.add_setting :use_instantiated_fixtures
  config.add_setting :global_fixtures

  if ::Rails::VERSION::STRING < "7.1.0"
    config.add_setting :fixture_path
  else
    config.add_setting :fixture_paths
  end

  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

  config.add_setting :file_fixture_path, default: 'spec/fixtures/files'
  config.include RSpec::Rails::FileFixtureSupport

  # 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

    undef :rendering_views? if respond_to?(:rendering_views?)
    def rendering_views?
      !!rendering_views
    end

    # Define boolean predicates rather than relying on rspec-core due
    # to the bug fix in rspec/rspec-core#2736, note some of these
    # predicates are a bit nonsensical, but they exist for backwards
    # compatibility, we can tidy these up in `rspec-rails` 5.
    undef :fixture_path? if respond_to?(:fixture_path?)
    def fixture_path?
      !!fixture_path
    end

    undef :global_fixtures? if respond_to?(:global_fixtures?)
    def global_fixtures?
      !!global_fixtures
    end

    undef :infer_base_class_for_anonymous_controllers? if respond_to?(:infer_base_class_for_anonymous_controllers?)
    def infer_base_class_for_anonymous_controllers?
      !!infer_base_class_for_anonymous_controllers
    end

    undef :use_instantiated_fixtures? if respond_to?(:use_instantiated_fixtures?)
    def use_instantiated_fixtures?
      !!use_instantiated_fixtures
    end

    undef :use_transactional_fixtures? if respond_to?(:use_transactional_fixtures?)
    def use_transactional_fixtures?
      !!use_transactional_fixtures
    end

    def infer_spec_type_from_file_location!
      DIRECTORY_MAPPINGS.each do |type, dir_parts|
        escaped_path = Regexp.compile(dir_parts.join('[\\\/]') + '[\\\/]')
        (file_path: escaped_path) do ||
          [: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

    # @deprecated TestFixtures#fixture_path is deprecated and will be removed in Rails 7.2
    if ::Rails::VERSION::STRING >= "7.1.0"
      def fixture_path
        RSpec.deprecate(
          "config.fixture_path",
          replacement: "config.fixture_paths",
          message: "Rails 7.1 has deprecated the singular fixture_path in favour of an array." \
          "You should migrate to plural:"
        )
        fixture_paths&.first
      end

      def fixture_path=(path)
        RSpec.deprecate(
          "config.fixture_path = #{path.inspect}",
          replacement: "config.fixture_paths = [#{path.inspect}]",
          message: "Rails 7.1 has deprecated the singular fixture_path in favour of an array." \
          "You should migrate to plural:"
        )
        self.fixture_paths = Array(path)
      end
    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 RSpec::Rails::FeatureCheck.has_action_mailer?
    config.include RSpec::Rails::MailerExampleGroup, type: :mailer
    config.after { ActionMailer::Base.deliveries.clear }
  end

  if RSpec::Rails::FeatureCheck.has_active_job?
    config.include RSpec::Rails::JobExampleGroup, type: :job
  end

  if RSpec::Rails::FeatureCheck.has_action_cable_testing?
    config.include RSpec::Rails::ChannelExampleGroup, type: :channel
  end

  if RSpec::Rails::FeatureCheck.has_action_mailbox?
    config.include RSpec::Rails::MailboxExampleGroup, type: :mailbox
  end
end

Instance Method Details

#infer_spec_type_from_file_location!

Automatically tag specs in conventional directories with matching type metadata so that they have relevant helpers available to them. See DIRECTORY_MAPPINGS for details on which metadata is applied to each directory.

[ GitHub ]

#render_views(val)

Enables view rendering for controllers specs.

[ GitHub ]

#render_views=(val)

When set to true, controller specs will render the relevant view as well. Defaults to false.

[ GitHub ]

#render_views?

Reader for currently value of #render_views setting.

[ GitHub ]