Skip to content

Commit

Permalink
Adds assert_invalid
Browse files Browse the repository at this point in the history
Allows asserting that the correct validations were added to a model.
Streamline the testing process for validations and make it easier for developers to understand the purpose of each test case.
  • Loading branch information
DanielaVelasquez committed May 10, 2024
1 parent 735da84 commit f7da59d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Adds `assert_invalid` to to validate a model has the right validations
```ruby
assert_invalid :name, :blank, :user
```
*Daniela Velasquez*

* Remove deprecated `ActiveSupport::Notifications::Event#children` and `ActiveSupport::Notifications::Event#parent_of?`.

*Rafael Mendonça França*
Expand Down
14 changes: 14 additions & 0 deletions activesupport/lib/active_support/testing/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ def assert_no_changes(expression, message = nil, from: UNTRACKED, &block)
retval
end

# Assertion that an active model has a specific invalid field
#
# assert_invalid :name, :blank, :user
def assert_invalid(attribute, type, obj, msg = nil)
raise ArgumentError.new("#{obj.inspect} does not respond to #validate") unless obj.respond_to?(:validate)

obj.validate
msg = message(msg) {
data = [attribute, type]
"Expected %s to be %s" % data
}
refute_empty obj.errors.where(attribute, type), msg
end

private
def _assert_nothing_raised_or_warn(assertion, &block)
assert_nothing_raised(&block)
Expand Down
42 changes: 42 additions & 0 deletions activesupport/test/active_model_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require_relative "abstract_unit"
require "active_model"

class ActiveModelTest < ActiveSupport::TestCase
def setup
@active_model = Class.new do
include ActiveModel::Model
include ActiveModel::Attributes

attribute :name, :string
validates :name, presence: true
end.new
end

test "#assert_invalid asserts active model validation is invalid" do
assert_invalid :name, :blank, @active_model
end

test "#assert_invalid raises ArgumentError with a non-active record" do
assert_raises(ArgumentError) do
assert_invalid :name, :blank, Object.new
end
end

test "#assert_invalid raises ArgumentError with message about the object nor responding to validate" do
error = assert_raises(ArgumentError) do
assert_invalid :name, :blank, Object.new
end

assert_includes error.message, "does not respond to #validate"
end

test "#assert_invalid raises a Minitest::Assertion when validation" do
@active_model.name = "h"
error = assert_raises(Minitest::Assertion) do
assert_invalid :name, :blank, @active_model
end
assert_includes error.message, "Expected name to be blank"
end
end

0 comments on commit f7da59d

Please sign in to comment.