/
route_to_spec.rb
127 lines (111 loc) · 4.17 KB
/
route_to_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
require "spec_helper"
describe "route_to" do
include RSpec::Rails::Matchers::RoutingMatchers
include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers
it "provides a description" do
matcher = route_to 'these' => 'options'
matcher.instance_variable_set(:@verb_to_path_map, { :get => 'path' })
matcher.description.should == "route {:get=>\"path\"} to {\"these\"=>\"options\"}"
end
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
it "routes with one query parameter" do
self.should_receive(:assert_recognizes).with(
{:controller => "controller", :action => "action", :queryitem => "queryvalue"},
{:method=> :get, :path=>"path" },
{'queryitem' => 'queryvalue' }
)
get("path?queryitem=queryvalue").should route_to("controller#action", :queryitem => 'queryvalue')
end
it "routes with multiple query parameters" do
self.should_receive(:assert_recognizes).with(
{:controller => "controller", :action => "action", :queryitem => "queryvalue", :qi2 => 'qv2'},
{:method=> :get, :path=>"path"},
{'queryitem' => 'queryvalue', 'qi2' => 'qv2'}
)
get("path?queryitem=queryvalue&qi2=qv2").should route_to("controller#action", :queryitem => 'queryvalue', :qi2 => 'qv2')
end
end
context "with should" do
context "when assert_recognizes passes" do
it "passes" do
self.stub!(:assert_recognizes)
expect do
{:get => "path"}.should route_to("these" => "options")
end.to_not raise_exception
end
end
context "when assert_recognizes fails" do
it "fails with message from assert_recognizes" do
self.stub!(:assert_recognizes) do
raise ActiveSupport::TestCase::Assertion.new("this message")
end
expect do
{:get => "path"}.should route_to("these" => "options")
end.to raise_error("this message")
end
end
context "when an exception is raised" do
it "raises that exception" do
self.stub!(:assert_recognizes) do
raise "oops"
end
expect do
{:get => "path"}.should route_to("these" => "options")
end.to raise_exception("oops")
end
end
end
context "with should_not" do
context "when assert_recognizes passes" do
it "fails with custom message" do
self.stub!(:assert_recognizes)
expect do
{:get => "path"}.should_not route_to("these" => "options")
end.to raise_error(/expected .* not to route to .*/)
end
end
context "when assert_recognizes fails" do
it "passes" do
self.stub!(:assert_recognizes) do
raise ActiveSupport::TestCase::Assertion.new("this message")
end
expect do
{:get => "path"}.should_not route_to("these" => "options")
end.to_not raise_error
end
end
context "when an exception is raised" do
it "raises that exception" do
self.stub!(:assert_recognizes) do
raise "oops"
end
expect do
{:get => "path"}.should_not route_to("these" => "options")
end.to raise_exception("oops")
end
end
end
it "uses failure message from assert_recognizes" do
self.stub!(:assert_recognizes).and_raise(
ActiveSupport::TestCase::Assertion.new("this message"))
expect do
{"this" => "path"}.should route_to("these" => "options")
end.to raise_error("this message")
end
end