aboutsummaryrefslogblamecommitdiffstats
path: root/actionpack/test/controller/required_params_test.rb
blob: 25d033721264b485f05467f05c983564474a0ffe (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                                     
                                                      

                                                     
 
                                                      

                                                  





                                                               
   







                                                                   
require 'abstract_unit'

class BooksController < ActionController::Base
  def create
    params.require(:book).require(:name)
    head :ok
  end
end

class ActionControllerRequiredParamsTest < ActionController::TestCase
  tests BooksController

  test "missing required parameters will raise exception" do
    assert_raise ActionController::ParameterMissing do
      post :create, { magazine: { name: "Mjallo!" } }
    end

    assert_raise ActionController::ParameterMissing do
      post :create, { book: { title: "Mjallo!" } }
    end
  end

  test "required parameters that are present will not raise" do
    post :create, { book: { name: "Mjallo!" } }
    assert_response :ok
  end
end

class ParametersRequireTest < ActiveSupport::TestCase
  test "required parameters must be present not merely not nil" do
    assert_raises(ActionController::ParameterMissing) do
      ActionController::Parameters.new(person: {}).require(:person)
    end
  end
end