Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
12f49762
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,发现更多精彩内容 >>
提交
12f49762
编写于
1月 12, 2016
作者:
R
Rafael França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #22935 from cllns/add-status-name-to-output
Add HTTP status name to output of tests
上级
89f70938
a4032ca0
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
84 addition
and
17 deletion
+84
-17
actionpack/CHANGELOG.md
actionpack/CHANGELOG.md
+4
-0
actionpack/lib/action_dispatch.rb
actionpack/lib/action_dispatch.rb
+1
-0
actionpack/lib/action_dispatch/testing/assertion_response.rb
actionpack/lib/action_dispatch/testing/assertion_response.rb
+49
-0
actionpack/lib/action_dispatch/testing/assertions/response.rb
...onpack/lib/action_dispatch/testing/assertions/response.rb
+14
-14
actionpack/test/assertions/response_assertions_test.rb
actionpack/test/assertions/response_assertions_test.rb
+16
-3
未找到文件。
actionpack/CHANGELOG.md
浏览文件 @
12f49762
*
Provide the name of HTTP Status code in assertions.
*Sean Collins*
*
More explicit error message when running
`rake routes`
.
`CONTROLLER`
argument
can now be supplied in different ways:
`Rails::WelcomeController`
,
`Rails::Welcome`
,
`rails/welcome`
...
...
actionpack/lib/action_dispatch.rb
浏览文件 @
12f49762
...
...
@@ -95,6 +95,7 @@ module Session
autoload
:TestProcess
autoload
:TestRequest
autoload
:TestResponse
autoload
:AssertionResponse
end
end
...
...
actionpack/lib/action_dispatch/testing/assertion_response.rb
0 → 100644
浏览文件 @
12f49762
module
ActionDispatch
# This is a class that abstracts away an asserted response.
# It purposely does not inherit from Response, because it doesn't need it.
# That means it does not have headers or a body.
#
# As an input to the initializer, we take a Fixnum, a String, or a Symbol.
# If it's a Fixnum or String, we figure out what its symbolized name.
# If it's a Symbol, we figure out what its corresponding code is.
# The resulting code will be a Fixnum, for real HTTP codes, and it will
# be a String for the pseudo-HTTP codes, such as:
# :success, :missing, :redirect and :error
class
AssertionResponse
attr_reader
:code
,
:name
GENERIC_RESPONSE_CODES
=
{
# :nodoc:
success:
"2XX"
,
missing:
"404"
,
redirect:
"3XX"
,
error:
"5XX"
}
def
initialize
(
code_or_name
)
if
code_or_name
.
is_a?
(
Symbol
)
@name
=
code_or_name
@code
=
code_from_name
(
code_or_name
)
else
@name
=
name_from_code
(
code_or_name
)
@code
=
code_or_name
end
raise
ArgumentError
,
"Invalid response name:
#{
name
}
"
if
@code
.
nil?
raise
ArgumentError
,
"Invalid response code:
#{
code
}
"
if
@name
.
nil?
end
def
code_and_name
"
#{
code
}
:
#{
name
}
"
end
private
def
code_from_name
(
name
)
GENERIC_RESPONSE_CODES
[
name
]
||
Rack
::
Utils
::
SYMBOL_TO_STATUS_CODE
[
name
]
end
def
name_from_code
(
code
)
GENERIC_RESPONSE_CODES
.
invert
[
code
]
||
Rack
::
Utils
::
HTTP_STATUS_CODES
[
code
]
end
end
end
actionpack/lib/action_dispatch/testing/assertions/response.rb
浏览文件 @
12f49762
module
ActionDispatch
module
Assertions
# A small suite of assertions that test responses from \Rails applications.
...
...
@@ -29,18 +28,10 @@ module ResponseAssertions
def
assert_response
(
type
,
message
=
nil
)
message
||=
generate_response_message
(
type
)
if
Symbol
===
type
if
[
:success
,
:missing
,
:redirect
,
:error
].
include?
(
type
)
if
RESPONSE_PREDICATES
.
keys
.
include?
(
type
)
assert
@response
.
send
(
RESPONSE_PREDICATES
[
type
]),
message
else
code
=
Rack
::
Utils
::
SYMBOL_TO_STATUS_CODE
[
type
]
if
code
.
nil?
raise
ArgumentError
,
"Invalid response type :
#{
type
}
"
end
assert_equal
code
,
@response
.
response_code
,
message
end
else
assert_equal
type
,
@response
.
response_code
,
message
assert_equal
AssertionResponse
.
new
(
type
).
code
,
@response
.
response_code
,
message
end
end
...
...
@@ -85,8 +76,9 @@ def normalize_argument_to_redirection(fragment)
end
end
def
generate_response_message
(
type
,
code
=
@response
.
response_code
)
"Expected response to be a <
#{
type
}
>, but was a <
#{
code
}
>"
def
generate_response_message
(
expected
,
actual
=
@response
.
response_code
)
"Expected response to be a <
#{
code_with_name
(
expected
)
}
>,"
\
" but was a <
#{
code_with_name
(
actual
)
}
>"
.
concat
location_if_redirected
end
...
...
@@ -95,6 +87,14 @@ def location_if_redirected
location
=
normalize_argument_to_redirection
(
@response
.
location
)
" redirect to <
#{
location
}
>"
end
def
code_with_name
(
code_or_name
)
if
RESPONSE_PREDICATES
.
values
.
include?
(
"
#{
code_or_name
}
?"
.
to_sym
)
code_or_name
=
RESPONSE_PREDICATES
.
invert
[
"
#{
code_or_name
}
?"
.
to_sym
]
end
AssertionResponse
.
new
(
code_or_name
).
code_and_name
end
end
end
end
actionpack/test/assertions/response_assertions_test.rb
浏览文件 @
12f49762
...
...
@@ -74,7 +74,18 @@ def test_error_message_shows_404_when_404_asserted_for_success
@response
.
status
=
404
error
=
assert_raises
(
Minitest
::
Assertion
)
{
assert_response
:success
}
expected
=
"Expected response to be a <success>, but was a <404>"
expected
=
"Expected response to be a <2XX: success>,"
\
" but was a <404: Not Found>"
assert_match
expected
,
error
.
message
end
def
test_error_message_shows_404_when_asserted_for_200
@response
=
ActionDispatch
::
Response
.
new
@response
.
status
=
404
error
=
assert_raises
(
Minitest
::
Assertion
)
{
assert_response
200
}
expected
=
"Expected response to be a <200: OK>,"
\
" but was a <404: Not Found>"
assert_match
expected
,
error
.
message
end
...
...
@@ -84,7 +95,8 @@ def test_error_message_shows_302_redirect_when_302_asserted_for_success
@response
.
location
=
'http://test.host/posts/redirect/1'
error
=
assert_raises
(
Minitest
::
Assertion
)
{
assert_response
:success
}
expected
=
"Expected response to be a <success>, but was a <302>"
\
expected
=
"Expected response to be a <2XX: success>,"
\
" but was a <302: Found>"
\
" redirect to <http://test.host/posts/redirect/1>"
assert_match
expected
,
error
.
message
end
...
...
@@ -95,7 +107,8 @@ def test_error_message_shows_302_redirect_when_302_asserted_for_301
@response
.
location
=
'http://test.host/posts/redirect/2'
error
=
assert_raises
(
Minitest
::
Assertion
)
{
assert_response
301
}
expected
=
"Expected response to be a <301>, but was a <302>"
\
expected
=
"Expected response to be a <301: Moved Permanently>,"
\
" but was a <302: Found>"
\
" redirect to <http://test.host/posts/redirect/2>"
assert_match
expected
,
error
.
message
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录