でアクセスできるアクションは 2 つだけProductsController
です。
# /config/routes.rb
RailsApp::Application.routes.draw do
resources :products, only: [:index, :show]
end
それに応じてテストが設定されます。
# /spec/controllers/products_controller_spec.rb
require 'spec_helper'
describe ProductsController do
before do
@product = Product.gen
end
describe "GET index" do
it "renders the index template" do
get :index
expect(response.status).to eq(200)
expect(response).to render_template(:index)
end
end
describe "GET show" do
it "renders the show template" do
get :show, id: @product.id
expect(response.status).to eq(200)
expect(response).to render_template(:show)
end
end
end
他のCRUD アクションにアクセスできないことをどのようにテストしますか? これは将来変更される可能性があるため、テストにより、構成の変更が確実に認識されるようになります。テストケースをカバーする有望なマッチャー
を見つけました。be_routable
コントローラーのアクションをテストするタイミングと理由について説明している Dave Newton によるこの投稿をお勧めします。