Performance: Controller tests
Controller tests impact development speed and test suite runtime, affecting how quickly developers get feedback.
test "index action returns success" do get :index, params: { format: :json } assert_response :success assert_includes @response.content_type, 'application/json' end
test "index action" do get :index assert_response :success assert_not_nil assigns(:items) assert_select 'div.item', minimum: 1 end
| Pattern | Database Queries | View Rendering | Test Runtime | Verdict |
|---|---|---|---|---|
| Full controller test with view rendering | Multiple queries | Full rendering | Slow (100+ ms) | [X] Bad |
| Controller test with JSON response and mocks | Minimal queries | No rendering | Fast (30 ms) | [OK] Good |
index action in a Rails controller test?get :action_name.get :index, which is correct syntax. Others are invalid or incorrect Ruby syntax.get :show, params: { id: 1 }
assert_response :success
assert_not_nil assigns(:user)assert_not_nil assigns(:user) check?assigns(:user) method accesses the instance variable @user set by the controller.assert_not_nil assigns(:user) checks that @user is not nil, meaning it was assigned.post :create, user: { name: 'Alice' }
assert_redirected_to user_path(assigns(:user))params: keyword.post :create, user: { name: 'Alice' } missing params:, causing syntax error.params: keyword, so post :create, params: { user: { name: 'Bob' } }
assert_redirected_to user_path(assigns(:user))
assert_equal 'User created!', flash[:notice] is correct here.Pass the test and this pattern moves unknown