-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds validations for active model errors
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
1 parent
2e7dc14
commit 0568062
Showing
5 changed files
with
204 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
* Adds `assert_error`, `assert_no_error`, `assert_valid` and `assert_not_valid` to ensure a model has the right validations | ||
```ruby | ||
assert_error :name, :blank, :user | ||
assert_no_error :name, :blank, :user | ||
assert_valid :name, :blank, :user | ||
assert_not_valid :name, :blank, :user | ||
``` | ||
|
||
*Daniela Velasquez* | ||
|
||
Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/activesupport/CHANGELOG.md) for previous changes. |
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,123 @@ | ||
# 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 | ||
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 asserts active model does not have an error on a field" do | ||
@active_model.name = "name" | ||
@active_model.validate | ||
|
||
assert_no_error :name, :blank, @active_model | ||
end | ||
|
||
test "#assert_no_error raises ArgumentError with a non-active record" do | ||
error = assert_raises(ArgumentError) do | ||
assert_no_error :name, :blank, Object.new | ||
end | ||
|
||
assert_includes error.message, "does not respond to #errors" | ||
end | ||
|
||
test "#assert_no_error raises a Minitest::Assertion when validation fails" do | ||
@active_model.validate | ||
error = assert_raises(Minitest::Assertion) do | ||
assert_no_error :name, :blank, @active_model | ||
end | ||
assert_includes error.message, "Expected name to not be blank" | ||
end | ||
|
||
test "#assert_error asserts active model has an error on a field" do | ||
@active_model.validate | ||
assert_error :name, :blank, @active_model | ||
end | ||
|
||
test "#assert_error 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 :name, error_message, @active_model | ||
end | ||
|
||
test "#assert_error raises ArgumentError with a non-active record" do | ||
error = assert_raises(ArgumentError) do | ||
assert_error :name, :blank, Object.new | ||
end | ||
|
||
assert_includes error.message, "does not respond to #errors" | ||
end | ||
|
||
test "#assert_error raises a Minitest::Assertion when validation fails" do | ||
@active_model.name = "h" | ||
@active_model.validate | ||
error = assert_raises(Minitest::Assertion) do | ||
assert_error :name, :blank, @active_model | ||
end | ||
assert_includes error.message, "Expected error blank on name" | ||
end | ||
|
||
test "#assert_not_valid asserts active model has error on a field after validation" do | ||
assert_not_valid :name, :blank, @active_model | ||
end | ||
|
||
test "#assert_not_valid asserts active model has error on a field with a string message" do | ||
@active_model.name = "Person1" | ||
assert_not_valid :name, "shouldn't contain numbers", @active_model | ||
end | ||
|
||
test "#assert_not_valid raises ArgumentError with message about the object nor responding to validate" do | ||
error = assert_raises(ArgumentError) do | ||
assert_not_valid :name, :blank, Object.new | ||
end | ||
|
||
assert_includes error.message, "does not respond to #validate" | ||
end | ||
|
||
test "#assert_not_valid raises a Minitest::Assertion when validation fails" do | ||
@active_model.name = "Hi" | ||
error = assert_raises(Minitest::Assertion) do | ||
assert_not_valid :name, :blank, @active_model | ||
end | ||
assert_includes error.message, "Expected error blank on name" | ||
end | ||
|
||
test "#assert_valid asserts active model does not have an error on a field after validation" do | ||
@active_model.name = "name" | ||
|
||
assert_valid :name, :blank, @active_model | ||
end | ||
|
||
test "#assert_valid raises ArgumentError with message about the object nor responding to validate" do | ||
error = assert_raises(ArgumentError) do | ||
assert_valid :name, :blank, Object.new | ||
end | ||
|
||
assert_includes error.message, "does not respond to #validate" | ||
end | ||
|
||
test "#assert_valid raises a Minitest::Assertion when validation fails" do | ||
error = assert_raises(Minitest::Assertion) do | ||
assert_valid :name, :blank, @active_model | ||
end | ||
assert_includes error.message, "Expected name to not be blank" | ||
end | ||
end |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
* Use Kamal for deployment by default, which includes generating a Rails-specific config/deploy.yml. | ||
This can be skipped using --skip-kamal. See more: https://kamal-deploy.org/ | ||
|
||
*DHH* | ||
*DHH* | ||
|
||
Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/railties/CHANGELOG.md) for previous changes. |