forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
assert_error_on
and assert_no_error_on
assertions
Introduces two new assertions, `assert_error_on` and `assert_no_error_on`, to simplify checking for specific validation errors on models. Example usage: - assert_error_on user, :name, :blank - assert_no_error_on user, :name, :blank This enhances test readability and makes validation testing more intuitive.
- Loading branch information
1 parent
b050366
commit ee2e91e
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "abstract_unit" | ||
require "active_model" | ||
|
||
class ValidationErrorsTest < ActiveSupport::TestCase | ||
def setup | ||
@active_model = Class.new do | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :name, :string | ||
attribute :last_name, :string | ||
validates :name, :last_name, presence: true | ||
validate :name_doesnt_contain_numbers | ||
|
||
private | ||
def name_doesnt_contain_numbers | ||
unless name.nil? || name.scan(/\d/).empty? | ||
errors.add(:name, "shouldn't contain numbers") | ||
end | ||
end | ||
end.new | ||
end | ||
|
||
test "#assert_no_error_on asserts active model does not have an error on a field" do | ||
@active_model.name = "name" | ||
@active_model.validate | ||
|
||
assert_no_error_on @active_model, :name, :blank | ||
end | ||
|
||
test "#assert_no_error_on raises ArgumentError with an object that doesn't respond to errors" do | ||
error = assert_raises(ArgumentError) do | ||
assert_no_error_on Object.new, :name, :blank, Object.new | ||
end | ||
|
||
assert_includes error.message, "does not respond to #errors" | ||
end | ||
|
||
test "#assert_no_error_on raises a Minitest::Assertion when validation fails" do | ||
@active_model.validate | ||
error = assert_raises(Minitest::Assertion) do | ||
assert_no_error_on @active_model, :name, :blank | ||
end | ||
assert_includes error.message, "Expected name to not be blank" | ||
end | ||
|
||
test "#assert_error_on asserts active model has an error on name field" do | ||
@active_model.validate | ||
assert_error_on @active_model, :name, :blank | ||
end | ||
|
||
test "#assert_error_on asserts active model has an error on a field with a string" do | ||
error_message = "must start with H" | ||
@active_model.errors.add(:name, error_message) | ||
|
||
assert_error_on @active_model, :name, error_message | ||
end | ||
|
||
test "#assert_error_on raises ArgumentError on an object that doesn't respond to errors" do | ||
error = assert_raises(ArgumentError) do | ||
assert_error_on :name, :blank, Object.new | ||
end | ||
|
||
assert_includes error.message, "does not respond to #errors" | ||
end | ||
|
||
test "#assert_error_on raises a Minitest::Assertion when validation fails" do | ||
@active_model.name = "h" | ||
@active_model.validate | ||
error = assert_raises(Minitest::Assertion) do | ||
assert_error_on @active_model, :name, :blank | ||
end | ||
assert_includes error.message, "Expected error blank on name" | ||
end | ||
end |