在 中只有两个可访问的操作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