Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions features/routing_specs/route_to_matcher.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Feature: route_to matcher

{ :get => "/" }.should route_to(:controller => "welcome")

or

get("/").should route_to("welcome#index")

Scenario: passing route spec
Given a file named "spec/routing/widgets_routing_spec.rb" with:
"""
Expand All @@ -21,6 +25,22 @@ Feature: route_to matcher
When I run "rspec spec/routing/widgets_routing_spec.rb"
Then the examples should all pass

Scenario: passing route spec
Given a file named "spec/routing/widgets_routing_spec.rb" with:
"""
require "spec_helper"

describe "routes for Widgets" do
it "routes /widgets to the widgets controller" do
get("/widgets").
should route_to("widgets#index")
end
end
"""

When I run "rspec spec/routing/widgets_routing_spec.rb"
Then the examples should all pass

Scenario: route spec for a route that doesn't exist (fails)
Given a file named "spec/routing/widgets_routing_spec.rb" with:
"""
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/example/routing_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module RoutingExampleGroup
include RSpec::Rails::RailsExampleGroup
include ActionDispatch::Assertions::RoutingAssertions
include RSpec::Rails::Matchers::RoutingMatchers
include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers

module InstanceMethods
attr_reader :routes
Expand Down
30 changes: 27 additions & 3 deletions lib/rspec/rails/matchers/routing_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@ module RSpec::Rails::Matchers
module RoutingMatchers
extend RSpec::Matchers::DSL

matcher :route_to do |route_options|
match_unless_raises ActiveSupport::TestCase::Assertion do |path|
matcher :route_to do |*route_options|
match_unless_raises Test::Unit::AssertionFailedError do |path|
assertion_path = { :method => path.keys.first, :path => path.values.first }
assert_recognizes(route_options, assertion_path)

path, options = *route_options

if path.is_a?(String)
parts = path.split("#")
options ||= {}
options.merge!({ :controller => parts.first, :action => parts.last })
else
options = path
end

assert_recognizes(options, assertion_path)
end

failure_message_for_should do
rescued_exception.message
end

end


matcher :be_routable do
match_unless_raises ActionController::RoutingError do |path|
@routing_options = routes.recognize_path(
Expand All @@ -24,5 +37,16 @@ module RoutingMatchers
"expected #{path.inspect} not to be routable, but it routes to #{@routing_options.inspect}"
end
end

module RouteHelpers

%w(get post put delete options head).each do |method|
define_method method do |path|
{ method.to_sym => path }
end
end

end

end
end
15 changes: 15 additions & 0 deletions spec/rspec/rails/matchers/route_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

describe "route_to" do
include RSpec::Rails::Matchers::RoutingMatchers
include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers

it "delegates to assert_recognizes" do
self.should_receive(:assert_recognizes).with({ "these" => "options" }, { :method=> :get, :path=>"path" })
{:get => "path"}.should route_to("these" => "options")
end

context "with shortcut syntax" do

it "routes with extra options" do
self.should_receive(:assert_recognizes).with({ :controller => "controller", :action => "action", :extra => "options"}, { :method=> :get, :path=>"path" })
get("path").should route_to("controller#action", :extra => "options")
end

it "routes without extra options" do
self.should_receive(:assert_recognizes).with({ :controller => "controller", :action => "action"}, { :method=> :get, :path=>"path" })
get("path").should route_to("controller#action")
end

end

context "with should" do
context "when assert_recognizes passes" do
it "passes" do
Expand Down