-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
735da84
commit f7da59d
Showing
3 changed files
with
62 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,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 |