Skip to content

Commit

Permalink
Merge pull request #668 from rspec/routing_specs_can_set_routes
Browse files Browse the repository at this point in the history
Allow routing specs to access engine routes
  • Loading branch information
alindeman committed Mar 1, 2013
2 parents 38488da + fd374e0 commit 2087db0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
37 changes: 37 additions & 0 deletions features/routing_specs/engine_routes.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Feature: engine routes

Routing specs can specify the routeset that will be used for the example
group. This is most useful when testing Rails engines.

Scenario: specify engine route
Given a file named "spec/routing/engine_routes_spec.rb" with:
"""ruby
require "spec_helper"
# A very simple Rails engine
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace MyEngine
end
Engine.routes.draw do
resources :widgets, only: [:index]
end
class WidgetsController < ::ActionController::Base
def index
end
end
end
describe MyEngine::WidgetsController do
routes { MyEngine::Engine.routes }
it "routes to the list of all widgets" do
expect(:get => widgets_path).
to route_to(:controller => "my_engine/widgets", :action => "index")
end
end
"""
When I run `rspec spec`
Then the examples should all pass
30 changes: 28 additions & 2 deletions lib/rspec/rails/example/routing_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,43 @@ module RoutingExampleGroup
include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers
include RSpec::Rails::AssertionDelegator.new(ActionDispatch::Assertions::RoutingAssertions)

module ClassMethods
# Specifies the routeset that will be used for the example group. This
# is most useful when testing Rails engines.
#
# @example
#
# describe MyEngine::PostsController do
# routes { MyEngine::Engine.routes }
#
# it "routes posts#index" do
# expect(:get => "/posts").to
# route_to(:controller => "my_engine/posts", :action => "index")
# end
# end
def routes(&blk)
before do
self.routes = blk.call
end
end
end

included do
metadata[:type] = :routing

before do
@routes = ::Rails.application.routes
assertion_instance.instance_variable_set(:@routes, @routes)
self.routes = ::Rails.application.routes
end
end

attr_reader :routes

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

private

def method_missing(m, *args, &block)
Expand Down

0 comments on commit 2087db0

Please sign in to comment.