module RSpec::Rails::ControllerExampleGroup

@api public Container module for controller spec functionality.

Attributes

controller[R]

@!attribute [r] Returns the controller object instance under test.

routes[R]

@!attribute [r] Returns the Rails routes used for the spec.

Public Instance Methods

bypass_rescue() click to toggle source

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.

@example

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

    expect do
      bypass_rescue
      get :show, :id => profile.id + 1
    end.to raise_error(/403 Forbidden/)
  end
end
# File lib/rspec/rails/example/controller_example_group.rb, line 166
def bypass_rescue
  controller.extend(BypassRescue)
end
method_missing(method, *args, &block) click to toggle source

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

Calls superclass method
# File 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
routes=(routes) click to toggle source

@private

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

# File lib/rspec/rails/example/controller_example_group.rb, line 137
def routes=(routes)
  @routes = routes
  assertion_instance.instance_variable_set(:@routes, routes)
end

Private Instance Methods

route_available?(method) click to toggle source
# File lib/rspec/rails/example/controller_example_group.rb, line 200
def route_available?(method)
  (defined?(@routes) && route_defined?(routes, method)) ||
    (defined?(@orig_routes) && route_defined?(@orig_routes, method))
end
route_defined?(routes, method) click to toggle source
# File lib/rspec/rails/example/controller_example_group.rb, line 205
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