From 2e3c6006ae56ea3270c5bec492313e4ddfaf4580 Mon Sep 17 00:00:00 2001 From: Nikhil Vengal Date: Tue, 7 Oct 2025 23:12:34 -0700 Subject: [PATCH] Add specs for /parks/:code --- config/routes.rb | 2 +- spec/requests/api/v1/parks_spec.rb | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 9a7a7f8..8b85eb2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do namespace :api do namespace :v1 do - resources :parks, only: %i[index] + resources :parks, only: %i[index show], param: :code end end # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html diff --git a/spec/requests/api/v1/parks_spec.rb b/spec/requests/api/v1/parks_spec.rb index f871160..ed646e2 100644 --- a/spec/requests/api/v1/parks_spec.rb +++ b/spec/requests/api/v1/parks_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' RSpec.describe "Api::V1::Parks", type: :request do - describe "GET /index" do + describe "GET /parks" do it "returns parks" do get api_v1_parks_url expect(response).to have_http_status(:success) @@ -34,4 +34,21 @@ RSpec.describe "Api::V1::Parks", type: :request do end end end + + describe "GET /parks/:code" do + it "returns :not_found for unknown codes" do + get api_v1_park_url("foo") + expect(response).to have_http_status(:not_found) + end + + it "returns the park" do + get api_v1_park_url("crla") + expect(response).to have_http_status(:success) + expect(response.parsed_body).to match( + hash_including( + code: "crla", name: "Crater Lake National Park", states: "OR" + ) + ) + end + end end