【发布时间】:2015-12-26 19:06:41
【问题描述】:
我有一个控制器方法来验证收到带有令牌的链接的用户(参见底部的方法)。我有一个集成测试:
def test
get login_path('invalid token') // Login_path routes to the controller method below.
assert flash[:danger]
assert_redirected_to root_path
end
此测试产生以下错误(参考get login_path('invalid token')):
ActionView::MissingTemplate: Missing template invitations/login, application/login with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.
视图invitiations/login 确实不存在。但是,考虑到下面的控制器方法,永远不需要这样的视图(它总是重定向到root_path 或呈现profiles/show)。什么可能导致此错误?
控制器方法:
def login
inv = Invitation.where('email = ?', params[:email])
if inv
inv.each do |person|
if person.authenticated?(:invitation, params[:id])
@organization = person.organization
unless @organization.nil?
render 'profiles/show' and return
else
flash[:danger] = "Error"
redirect_to root_path and return
end
end
flash[:danger] = "Invalid link"
redirect_to root_path
end
else
flash[:danger] = "Invalid link"
redirect_to root_path
end
end
附:该测试过去通过,即直到我重写了控制器方法以适应多个inv(参见Retrieve multiple records with find_by method)。
【问题讨论】:
-
渲染后你没有有返回。只要确保你没有渲染两次。在
if-conditional 的每个分支中渲染一次是可以的。 -
关于可能的错误来源的说明:您迭代了邀请,但 item 变量名为
person。我本来希望该变量被命名为invitation。而inv是invitations。遵循这种命名约定将为您省去很多麻烦。
标签: ruby-on-rails ruby model-view-controller integration-testing