无效的 link 在集成测试中崩溃而不是产生一个 flash 错误消息
Invalid link crashes in integration test instead of producing a flash error message
在集成测试中我有:
get activation_path("invalid token")
assert flash[:danger]
assert_redirected_to root_path
activation_path
指的是验证令牌的路径。正确的路径如下所示:get activation_path("valid token", inv: '10')
。因此在测试中,link 不包含 inv
的值,因此是无效的 link。控制器方法使用 inv
查找邀请:
@invitation = Invitation.find(params[:inv])
因此,预期的行为是无效的 link 会产生闪存错误和重定向。但是,测试的第一行失败并出现错误:
ActiveRecord::RecordNotFound: Couldn't find Invitation with 'id'=
这是指控制器方法的第一行,如前所述:
@invitation = Invitation.find(params[:inv])
为什么测试失败了?我希望它只是 return nil
for @invitation
但它会生成错误。我应该如何调整(控制器方法?)以考虑提交此类无效 links 的用户?
更新:我也试过了:
get activation_path("valid token", inv: '88888888888')
assert flash[:danger] # There's no invitation with that number
assert_redirected_to root_path
此测试也未能指向控制器方法的同一第一行:
ActiveRecord::RecordNotFound: Couldn't find Invitation with an out
of range value for 'id'
根据 Rails docs:
The find method will raise an ActiveRecord::RecordNotFound exception
if no matching record is found.
默认情况下,Rails 通过在生产和暂存中呈现 404 响应来响应 ActiveRecord::RecordNotFound
错误。当用户访问 URL 以获得不存在的记录时,这通常是所需的响应。
如果您希望它 return nil
而不是引发错误,请改用 find_by_id
:
Invitation.find_by_id 0
=> nil
在集成测试中我有:
get activation_path("invalid token")
assert flash[:danger]
assert_redirected_to root_path
activation_path
指的是验证令牌的路径。正确的路径如下所示:get activation_path("valid token", inv: '10')
。因此在测试中,link 不包含 inv
的值,因此是无效的 link。控制器方法使用 inv
查找邀请:
@invitation = Invitation.find(params[:inv])
因此,预期的行为是无效的 link 会产生闪存错误和重定向。但是,测试的第一行失败并出现错误:
ActiveRecord::RecordNotFound: Couldn't find Invitation with 'id'=
这是指控制器方法的第一行,如前所述:
@invitation = Invitation.find(params[:inv])
为什么测试失败了?我希望它只是 return nil
for @invitation
但它会生成错误。我应该如何调整(控制器方法?)以考虑提交此类无效 links 的用户?
更新:我也试过了:
get activation_path("valid token", inv: '88888888888')
assert flash[:danger] # There's no invitation with that number
assert_redirected_to root_path
此测试也未能指向控制器方法的同一第一行:
ActiveRecord::RecordNotFound: Couldn't find Invitation with an out of range value for 'id'
根据 Rails docs:
The find method will raise an ActiveRecord::RecordNotFound exception if no matching record is found.
默认情况下,Rails 通过在生产和暂存中呈现 404 响应来响应 ActiveRecord::RecordNotFound
错误。当用户访问 URL 以获得不存在的记录时,这通常是所需的响应。
如果您希望它 return nil
而不是引发错误,请改用 find_by_id
:
Invitation.find_by_id 0
=> nil