features/controller_specs/README.md
Controller specs are marked by type: :controller or if you have set
config.infer_spec_type_from_file_location! by placing them in spec/controllers.
A controller spec is an RSpec wrapper for a Rails functional test (ActionController::TestCase::Behavior). It allows you to simulate a single http request in each example, and then specify expected outcomes such as:
To specify outcomes, you can use:
expect(response.status).to eq(200))assert_equal 200, response.status)assert_response 200)expect(response).to render_template(:new) # wraps assert_template
expect(response).to redirect_to(location) # wraps assert_redirected_to
expect(response).to have_http_status(:created)
expect(assigns(:widget)).to be_a_new(Widget)
RSpec.describe TeamsController do
describe "GET index" do
it "assigns @teams" do
team = Team.create
get :index
expect(assigns(:teams)).to eq([team])
end
it "renders the index template" do
get :index
expect(response).to render_template("index")
end
end
end
We encourage you to use request specs if you want to set headers in your call. If you still want to use controller specs with custom http headers you can use request.headers:
require "rails_helper"
RSpec.describe TeamsController, type: :controller do
describe "GET index" do
it "returns a 200" do
request.headers["Authorization"] = "foo"
get :show
expect(response).to have_http_status(:ok)
end
end
end