2012-07-12 01:50:42 -04:00
|
|
|
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
|
2013-01-16 06:37:13 -05:00
|
|
|
assert_raise ActionController::ParameterMissing do
|
2013-01-15 19:07:13 -05:00
|
|
|
post :create, { magazine: { name: "Mjallo!" } }
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
|
2013-01-16 06:37:13 -05:00
|
|
|
assert_raise ActionController::ParameterMissing do
|
2013-01-15 19:07:13 -05:00
|
|
|
post :create, { book: { title: "Mjallo!" } }
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "required parameters that are present will not raise" do
|
|
|
|
post :create, { book: { name: "Mjallo!" } }
|
|
|
|
assert_response :ok
|
|
|
|
end
|
2014-06-13 13:42:38 -04:00
|
|
|
|
|
|
|
test "required parameters with false value will not raise" do
|
|
|
|
post :create, { book: { name: false } }
|
|
|
|
assert_response :ok
|
|
|
|
end
|
2012-07-12 01:50:42 -04:00
|
|
|
end
|
2014-01-23 22:28:24 -05:00
|
|
|
|
|
|
|
class ParametersRequireTest < ActiveSupport::TestCase
|
2014-06-13 13:42:38 -04:00
|
|
|
|
|
|
|
test "required parameters should accept and return false value" do
|
2014-06-13 16:23:45 -04:00
|
|
|
assert_equal(false, ActionController::Parameters.new(person: false).require(:person))
|
2014-06-13 13:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "required parameters must not be nil" do
|
|
|
|
assert_raises(ActionController::ParameterMissing) do
|
|
|
|
ActionController::Parameters.new(person: nil).require(:person)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "required parameters must not be empty" do
|
2014-01-23 22:28:24 -05:00
|
|
|
assert_raises(ActionController::ParameterMissing) do
|
|
|
|
ActionController::Parameters.new(person: {}).require(:person)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|