Skip to content

Commit

Permalink
Follow new behavior of Rails 6.1
Browse files Browse the repository at this point in the history
`ActiveModel::Errors#messages` now only returns error attributes.
  • Loading branch information
y-yagi committed Feb 10, 2021
1 parent 4762348 commit 4f89aa9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 10 additions & 2 deletions test/activemodel_validation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,11 @@ def email_present?
# valid.
it "is valid" do
_(form.validate({ username: "not yo", email: "bla" })).must_equal true
_(form.errors.messages).must_equal({:username=>[], :email=>[]})
if self.class.rails_greater_6_0?
_(form.errors.messages).must_equal({})
else
_(form.errors.messages).must_equal({:username=>[], :email=>[]})
end
if self.class.rails5?
_(form.errors.details.inspect).must_equal "{}"
end
Expand Down Expand Up @@ -312,7 +316,11 @@ def email_present?
_(form.errors.messages).must_equal(email: ["can't be blank", "fill it out!"], username: ["not ok", "must be yo"], policy: ["error_text", "another error"])
# keep added errors after validate
_(form.validate(username: "username", email: "[email protected]")).must_equal false
_(form.errors.messages).must_equal(policy: ["error_text", "another error"], username: [], email: [])
if self.class.rails_greater_6_0?
_(form.errors.messages).must_equal(policy: ["error_text", "another error"])
else
_(form.errors.messages).must_equal(policy: ["error_text", "another error"], username: [], email: [])
end
_(form.errors.added?(:policy, "error_text")).must_equal true
_(form.errors.added?(:policy, "another error")).must_equal true
_(form.errors.details).must_equal(
Expand Down
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class Album < ActiveRecord::Base
load "#{File.dirname(__FILE__)}/support/schema.rb"

Minitest::Spec.class_eval do
def self.rails_greater_6_0?
(::ActiveModel::VERSION::MAJOR == 6 and ::ActiveModel::VERSION::MINOR >= 1) || (::ActiveModel::VERSION::MAJOR >= 7)
end

def self.rails5?
::ActiveModel::VERSION::MAJOR.in? [5,6]
end
Expand Down

0 comments on commit 4f89aa9

Please sign in to comment.