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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
RSpec.describe "route_to" do
include RSpec::Rails::Matchers::RoutingMatchers
include RSpec::Rails::Matchers::RoutingMatchers::RouteHelpers
def assert_recognizes(*)
# no-op
end
it "provides a description" do
matcher = route_to("these" => "options")
matcher.matches?(get: "path")
description =
if RUBY_VERSION >= '3.4'
"route {get: \"path\"} to {\"these\" => \"options\"}"
else
"route {:get=>\"path\"} to {\"these\"=>\"options\"}"
end
expect(matcher.description).to eq(description)
end
it "delegates to assert_recognizes" do
expect(self).to receive(:assert_recognizes).with({ "these" => "options" }, { method: :get, path: "path" }, {})
expect({ get: "path" }).to route_to("these" => "options")
end
context "with shortcut syntax" do
it "routes with extra options" do
expect(self).to receive(:assert_recognizes).with({ controller: "controller", action: "action", extra: "options" }, { method: :get, path: "path" }, {})
expect(get("path")).to route_to("controller#action", extra: "options")
end
it "routes without extra options" do
expect(self).to receive(:assert_recognizes).with(
{ controller: "controller", action: "action" },
{ method: :get, path: "path" },
{}
)
expect(get("path")).to route_to("controller#action")
end
it "routes with one query parameter" do
expect(self).to receive(:assert_recognizes).with(
{ controller: "controller", action: "action", queryitem: "queryvalue" },
{ method: :get, path: "path" },
{ 'queryitem' => 'queryvalue' }
)
expect(get("path?queryitem=queryvalue")).to route_to("controller#action", queryitem: 'queryvalue')
end
it "routes with multiple query parameters" do
expect(self).to receive(:assert_recognizes).with(
{ controller: "controller", action: "action", queryitem: "queryvalue", qi2: 'qv2' },
{ method: :get, path: "path" },
{ 'queryitem' => 'queryvalue', 'qi2' => 'qv2' }
)
expect(get("path?queryitem=queryvalue&qi2=qv2")).to route_to("controller#action", queryitem: 'queryvalue', qi2: 'qv2')
end
it "routes with nested query parameters" do
expect(self).to receive(:assert_recognizes).with(
{ :controller => "controller", :action => "action", 'queryitem' => { 'qi2' => 'qv2' } },
{ method: :get, path: "path" },
{ 'queryitem' => { 'qi2' => 'qv2' } }
)
expect(get("path?queryitem[qi2]=qv2")).to route_to("controller#action", 'queryitem' => { 'qi2' => 'qv2' })
end
end
context "with should" do
context "when assert_recognizes passes" do
it "passes" do
expect do
expect({ get: "path" }).to route_to("these" => "options")
end.to_not raise_exception
end
end
context "when assert_recognizes fails with an assertion failure" do
it "fails with message from assert_recognizes" do
def assert_recognizes(*)
raise ActiveSupport::TestCase::Assertion, "this message"
end
expect do
expect({ get: "path" }).to route_to("these" => "options")
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "this message")
end
end
context "when assert_recognizes fails with a routing error" do
it "fails with message from assert_recognizes" do
def assert_recognizes(*)
raise ActionController::RoutingError, "this message"
end
expect do
expect({ get: "path" }).to route_to("these" => "options")
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, "this message")
end
end
context "when an exception is raised" do
it "raises that exception" do
def assert_recognizes(*)
raise "oops"
end
expect do
expect({ get: "path" }).to 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
message =
if RUBY_VERSION >= '3.4'
/expected \{get: "path"\} not to route to \{"these" => "options"\}/
else
/expected \{:get=>"path"\} not to route to \{"these"=>"options"\}/
end
expect {
expect({ get: "path" }).not_to route_to("these" => "options")
}.to raise_error(message)
end
end
context "when assert_recognizes fails with an assertion failure" do
it "passes" do
def assert_recognizes(*)
raise ActiveSupport::TestCase::Assertion, "this message"
end
expect do
expect({ get: "path" }).not_to route_to("these" => "options")
end.to_not raise_error
end
end
context "when assert_recognizes fails with a routing error" do
it "passes" do
def assert_recognizes(*)
raise ActionController::RoutingError, "this message"
end
expect do
expect({ get: "path" }).not_to route_to("these" => "options")
end.to_not raise_error
end
end
context "when an exception is raised" do
it "raises that exception" do
def assert_recognizes(*)
raise "oops"
end
expect do
expect({ get: "path" }).not_to route_to("these" => "options")
end.to raise_exception("oops")
end
end
end
it "uses failure message from assert_recognizes" do
def assert_recognizes(*)
raise ActiveSupport::TestCase::Assertion, "this message"
end
expect do
expect({ "this" => "path" }).to route_to("these" => "options")
end.to raise_error("this message")
end
end
|