123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Rails::ControllerExampleGroup

Relationships & Source Files
Namespace Children
Modules:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, RSpec::Rails::AssertionDelegator.new( ActionDispatch::Assertions::RoutingAssertions ), Matchers::RoutingMatchers, Matchers::RenderTemplate, Matchers::RedirectTo, ViewRendering, ActionController::TestCase::Behavior, RailsExampleGroup, ActiveSupport::ExecutionContext::TestHelper, ActiveSupport::CurrentAttributes::TestHelper, TaggedLoggingAdapter, FixtureSupport::Fixtures, FixtureSupport, ActiveRecord::TestFixtures, MinitestAssertionAdapter, MinitestLifecycleAdapter, SetupAndTeardownAdapter
Defined in: rspec-rails/lib/rspec/rails/example/controller_example_group.rb

Overview

Container module for controller spec functionality.

Constant Summary

ViewRendering - Included

RESOLVER_CACHE

Class Attribute Summary

Class Method Summary

ViewRendering::ClassMethods - Extended

MinitestAssertionAdapter::ClassMethods - Extended

assertion_method_names

Returns the names of assertion methods that we want to expose to examples without exposing non-assertion methods in Test::Unit or Minitest.

define_assertion_delegators

SetupAndTeardownAdapter::ClassMethods - Extended

setup

Wraps ‘setup` calls from within Rails’ testing framework in ‘before` hooks.

teardown

Wraps ‘teardown` calls from within Rails’ testing framework in ‘after` hooks.

Instance Attribute Summary

ViewRendering - Included

#controller

Returns the controller object instance under test.

#render_views?, #controller=

FixtureSupport - Included

#run_in_transaction?

Monkey patched to avoid collisions with ‘let(:name)’ in ::RSpec::Rails 6.1 and after and let(:method_name) before ::RSpec::Rails 6.1.

Instance Method Summary

Matchers::RoutingMatchers - Included

#be_routable

Passes if the route expression is recognized by the ::RSpec::Rails router based on the declarations in ‘config/routes.rb`.

#route_to

Delegates to ‘assert_recognizes`.

Matchers::RenderTemplate - Included

#have_rendered

Delegates to ‘assert_template`.

#render_template

Matchers::RedirectTo - Included

#redirect_to

Delegates to ‘assert_redirected_to`.

TaggedLoggingAdapter - Included

#tagged_logger

Vendored from activesupport/lib/active_support/testing/tagged_logging.rb This implements the tagged_logger method where it is expected, but doesn’t call ‘name` or set it up like ::RSpec::Rails does.

MinitestAssertionAdapter - Included

MinitestLifecycleAdapter - Included

SetupAndTeardownAdapter - Included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block)

If method is a named_route, delegates to the RouteSet associated with this controller.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 172

def method_missing(method, *args, &block)
  if route_available?(method)
    controller.send(method, *args, &block)
  else
    super
  end
end

DSL Calls

included

[ GitHub ]


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 181

included do
  subject { controller }

  before do
    self.routes = ::Rails.application.routes
  end

  around do |ex|
    previous_allow_forgery_protection_value = ActionController::Base.allow_forgery_protection
    begin
      ActionController::Base.allow_forgery_protection = false
      ex.call
    ensure
      ActionController::Base.allow_forgery_protection = previous_allow_forgery_protection_value
    end
  end
end

Instance Attribute Details

#controller (readonly)

Returns the controller object instance under test.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 128

attr_reader :controller

#routes (rw)

Returns the ::RSpec::Rails routes used for the spec.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 132

attr_reader :routes

#routes=(routes) (rw)

This method is for internal use only.

::RSpec Rails uses this to make ::RSpec::Rails routes easily available to specs.

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 137

def routes=(routes)
  @routes = routes
  assertion_instance.instance_variable_set(:@routes, routes)
end

Instance Method Details

#bypass_rescue

Extends the controller with a module that overrides ‘rescue_with_handler` to raise the exception passed to it. Use this to specify that an action should raise an exception given appropriate conditions.

Examples:

describe ProfilesController do
  it "raises a 403 when a non-admin user tries to view another user's profile" do
    profile = create_profile
     profile.user

    expect do
      bypass_rescue
      get :show, id: profile.id + 1
    end.to raise_error(/403 Forbidden/)
  end
end
[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 166

def bypass_rescue
  controller.extend(BypassRescue)
end

#route_available?(method) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 201

def route_available?(method)
  (defined?(@routes) && route_defined?(routes, method)) ||
    (defined?(@orig_routes) && route_defined?(@orig_routes, method))
end

#route_defined?(routes, method) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-rails/lib/rspec/rails/example/controller_example_group.rb', line 206

def route_defined?(routes, method)
  return false if routes.nil?

  if routes.named_routes.respond_to?(:route_defined?)
    routes.named_routes.route_defined?(method)
  else
    routes.named_routes.helpers.include?(method)
  end
end