Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2e585e40
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
2e585e40
编写于
11月 06, 2017
作者:
R
Rafael França
提交者:
GitHub
11月 06, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #31061 from bogdanvlviv/test-if-unless-options-for-validations
Add cases to test combining validation conditions
上级
212481b4
3ff2c158
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
62 addition
and
7 deletion
+62
-7
activemodel/test/cases/validations/conditional_validation_test.rb
...del/test/cases/validations/conditional_validation_test.rb
+47
-2
activemodel/test/cases/validations/numericality_validation_test.rb
...el/test/cases/validations/numericality_validation_test.rb
+1
-1
activemodel/test/cases/validations/validates_test.rb
activemodel/test/cases/validations/validates_test.rb
+9
-3
activemodel/test/models/person.rb
activemodel/test/models/person.rb
+4
-0
activemodel/test/models/topic.rb
activemodel/test/models/topic.rb
+1
-1
未找到文件。
activemodel/test/cases/validations/conditional_validation_test.rb
浏览文件 @
2e585e40
...
...
@@ -18,6 +18,22 @@ def test_if_validation_using_method_true
assert_equal
[
"hoo 5"
],
t
.
errors
[
"title"
]
end
def
test_if_validation_using_array_of_true_methods
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
if:
[
:condition_is_true
,
:condition_is_true
])
t
=
Topic
.
new
(
"title"
=>
"uhohuhoh"
,
"content"
=>
"whatever"
)
assert
t
.
invalid?
assert
t
.
errors
[
:title
].
any?
assert_equal
[
"hoo 5"
],
t
.
errors
[
"title"
]
end
def
test_unless_validation_using_array_of_false_methods
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
unless:
[
:condition_is_false
,
:condition_is_false
])
t
=
Topic
.
new
(
"title"
=>
"uhohuhoh"
,
"content"
=>
"whatever"
)
assert
t
.
invalid?
assert
t
.
errors
[
:title
].
any?
assert_equal
[
"hoo 5"
],
t
.
errors
[
"title"
]
end
def
test_unless_validation_using_method_true
# When the method returns true
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
unless: :condition_is_true
)
...
...
@@ -26,9 +42,23 @@ def test_unless_validation_using_method_true
assert_empty
t
.
errors
[
:title
]
end
def
test_if_validation_using_array_of_true_and_false_methods
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
if:
[
:condition_is_true
,
:condition_is_false
])
t
=
Topic
.
new
(
"title"
=>
"uhohuhoh"
,
"content"
=>
"whatever"
)
assert
t
.
valid?
assert_empty
t
.
errors
[
:title
]
end
def
test_unless_validation_using_array_of_true_and_felse_methods
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
unless:
[
:condition_is_true
,
:condition_is_false
])
t
=
Topic
.
new
(
"title"
=>
"uhohuhoh"
,
"content"
=>
"whatever"
)
assert
t
.
valid?
assert_empty
t
.
errors
[
:title
]
end
def
test_if_validation_using_method_false
# When the method returns false
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
if: :condition_is_
true_but_its_not
)
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
if: :condition_is_
false
)
t
=
Topic
.
new
(
"title"
=>
"uhohuhoh"
,
"content"
=>
"whatever"
)
assert
t
.
valid?
assert_empty
t
.
errors
[
:title
]
...
...
@@ -36,7 +66,7 @@ def test_if_validation_using_method_false
def
test_unless_validation_using_method_false
# When the method returns false
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
unless: :condition_is_
true_but_its_not
)
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
unless: :condition_is_
false
)
t
=
Topic
.
new
(
"title"
=>
"uhohuhoh"
,
"content"
=>
"whatever"
)
assert
t
.
invalid?
assert
t
.
errors
[
:title
].
any?
...
...
@@ -80,4 +110,19 @@ def test_unless_validation_using_block_false
assert
t
.
errors
[
:title
].
any?
assert_equal
[
"hoo 5"
],
t
.
errors
[
"title"
]
end
def
test_validation_using_conbining_if_true_and_unless_true_conditions
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
if: :condition_is_true
,
unless: :condition_is_true
)
t
=
Topic
.
new
(
"title"
=>
"uhohuhoh"
,
"content"
=>
"whatever"
)
assert
t
.
valid?
assert_empty
t
.
errors
[
:title
]
end
def
test_validation_using_conbining_if_true_and_unless_false_conditions
Topic
.
validates_length_of
(
:title
,
maximum:
5
,
too_long:
"hoo %{count}"
,
if: :condition_is_true
,
unless: :condition_is_false
)
t
=
Topic
.
new
(
"title"
=>
"uhohuhoh"
,
"content"
=>
"whatever"
)
assert
t
.
invalid?
assert
t
.
errors
[
:title
].
any?
assert_equal
[
"hoo 5"
],
t
.
errors
[
"title"
]
end
end
activemodel/test/cases/validations/numericality_validation_test.rb
浏览文件 @
2e585e40
...
...
@@ -59,7 +59,7 @@ def test_validates_numericality_of_with_integer_only_and_nil_allowed
end
def
test_validates_numericality_of_with_integer_only_and_symbol_as_value
Topic
.
validates_numericality_of
:approved
,
only_integer: :condition_is_
true_but_its_not
Topic
.
validates_numericality_of
:approved
,
only_integer: :condition_is_
false
invalid!
(
NIL
+
BLANK
+
JUNK
)
valid!
(
FLOATS
+
INTEGERS
+
BIGDECIMAL
+
INFINITY
)
...
...
activemodel/test/cases/validations/validates_test.rb
浏览文件 @
2e585e40
...
...
@@ -62,17 +62,23 @@ def test_validates_with_namespaced_validator_class
end
def
test_validates_with_if_as_local_conditions
Person
.
validates
:karma
,
presence:
true
,
email:
{
unless: :condition_is_tru
e
}
Person
.
validates
:karma
,
presence:
true
,
email:
{
if: :condition_is_fals
e
}
person
=
Person
.
new
person
.
valid?
assert_equal
[
"can't be blank"
],
person
.
errors
[
:karma
]
end
def
test_validates_with_if_as_shared_conditions
Person
.
validates
:karma
,
presence:
true
,
email:
true
,
if: :condition_is_true
Person
.
validates
:karma
,
presence:
true
,
email:
true
,
if: :condition_is_false
person
=
Person
.
new
assert
person
.
valid?
end
def
test_validates_with_unless_as_local_conditions
Person
.
validates
:karma
,
presence:
true
,
email:
{
unless: :condition_is_true
}
person
=
Person
.
new
person
.
valid?
assert_equal
[
"can't be blank"
,
"is not an email"
],
person
.
errors
[
:karma
].
sort
assert_equal
[
"can't be blank"
],
person
.
errors
[
:karma
]
end
def
test_validates_with_unless_shared_conditions
...
...
activemodel/test/models/person.rb
浏览文件 @
2e585e40
...
...
@@ -9,6 +9,10 @@ class Person
def
condition_is_true
true
end
def
condition_is_false
false
end
end
class
Person::Gender
...
...
activemodel/test/models/topic.rb
浏览文件 @
2e585e40
...
...
@@ -23,7 +23,7 @@ def condition_is_true
true
end
def
condition_is_
true_but_its_not
def
condition_is_
false
false
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录