Fix RoutePareser#routes and add spec for it

This commit is contained in:
Graceful Potato 2020-05-24 21:03:40 +03:00
parent 7ceedab4cb
commit 3b85f09acf
2 changed files with 51 additions and 1 deletions

View File

@ -11,7 +11,7 @@ module Rswag
def routes
::Rails.application.routes.routes.select do |route|
route.defaults[:controller] == controller
end.each_with_object({}) do |tree, route|
end.each_with_object({}) do |route, tree|
path = path_from(route)
verb = verb_from(route)
tree[path] ||= { params: params_from(route), actions: {} }

View File

@ -0,0 +1,50 @@
# frozen_string_literal: true
RSpec.describe Rswag::RouteParser do
describe "#routes" do
let(:controller) { "api/v1/posts" }
subject { described_class.new(controller) }
let(:routes) do
[
double(
defaults: {
controller: controller
},
path: double(
spec: double(
to_s: "/api/v1/posts/:id(.:format)"
)
),
verb: "GET",
requirements: {
action: "show",
controller: "api/v1/posts"
},
segments: ["id", "format"]
)
]
end
let(:expectation) do
{
"/api/v1/posts/{id}" => {
params: ["id"],
actions: {
"get" => {
summary: "show post"
}
}
}
}
end
before do
allow(::Rails).to receive_message_chain("application.routes.routes") { routes }
end
it "returns correct routes" do
expect(subject.routes).to eq(expectation)
end
end
end