Fix full error message test on Rails 6

Before Rails 6, `#human_attribute_name` was called with an symbol as the
first argument (`:name`) which made the switch case execute the `else`
branch (calling `attribute.to_s.humanize`). Ref: https://github.com/rails/rails/blob/5-2-stable/activemodel/lib/active_model/errors.rb#L370

On Rails 6, `#human_attribute_name` is always called with a String
(`"name"`) which made our switch case to execute the first branch.
Since the attribute name was not the most important part of the test -
the full error message is what we care about - I decided to change the
test in order to make it work on both versions.

References:
- https://github.com/rails/rails/blob/6-0-stable/activemodel/lib/active_model/errors.rb#L414
- https://github.com/rails/rails/blob/6-0-stable/activemodel/lib/active_model/errors.rb#L448
This commit is contained in:
Leonardo Tegon 2019-09-20 11:18:37 -03:00
parent 13d0341bdf
commit 8d56636ccf
2 changed files with 2 additions and 2 deletions

View File

@ -161,7 +161,7 @@ class WrapperTest < ActionView::TestCase
test 'custom wrappers can have full error message on attributes' do
swap_wrapper :default, custom_wrapper_with_full_error do
with_form_for @user, :name
assert_select 'span.error', "Name cannot be blank"
assert_select 'span.error', "Super User Name! cannot be blank"
end
end

View File

@ -204,7 +204,7 @@ class User
def self.human_attribute_name(attribute, options = {})
case attribute
when 'name'
when 'name', :name
'Super User Name!'
when 'description'
'User Description!'