如何在 Rails 的单元测试中否定 assert_response
How to negative assert_response in unit testing with Rails
如何测试 HTTP 响应是否 而不是 标准单元测试框架中的 Rails(更具体地说是版本 6.0)?基本上它是对 assert_response 的否定,像这样:
test "should get index" do
get restricted_articles_url
assert_response_NOT :success # <= this method does not exist.
end
背景是,测试的用户不应被授予访问 URL(或 Controller 方法)的权限,但返回的响应可能会在开发后期发生变化,准确的响应是次要的.
Ref rails-specific-assertions,你为什么不使用你期望的确切响应而不是使用 assert_response 的否定。
Asserts that the response comes with a specific status code. You can specify
:success to indicate 200-299, :redirect to indicate 300-399, :missing to
indicate 404, or :error to match the 500-599 range
例如:-
assert_response :redirect
您随时可以直接用
检查response.code
# codes from 200 to 299 are :success
assert_not (200...299).include?(response.code.to_i)
如何测试 HTTP 响应是否 而不是 标准单元测试框架中的 Rails(更具体地说是版本 6.0)?基本上它是对 assert_response 的否定,像这样:
test "should get index" do
get restricted_articles_url
assert_response_NOT :success # <= this method does not exist.
end
背景是,测试的用户不应被授予访问 URL(或 Controller 方法)的权限,但返回的响应可能会在开发后期发生变化,准确的响应是次要的.
Ref rails-specific-assertions,你为什么不使用你期望的确切响应而不是使用 assert_response 的否定。
Asserts that the response comes with a specific status code. You can specify
:success to indicate 200-299, :redirect to indicate 300-399, :missing to
indicate 404, or :error to match the 500-599 range
例如:-
assert_response :redirect
您随时可以直接用
检查response.code
# codes from 200 to 299 are :success
assert_not (200...299).include?(response.code.to_i)