我不知道在哪里放这篇文章,但这个主题更类似于我的问题 .
我有一个登录系统,像这样:
ApplicationController:包含SessionsHelper
私人的
helper_method:current_user
如果session [:user_id]结束,则def current_user @current_user || = User.find(session [:user_id])
SessionsHelper:def is_admin? @current_user && @ current_user.id == 1 end features / support / env.rb:World(SessionsHelper)
所以,如果我测试
is_admin? . 应该是be_true
它返回:
预期nil为真(RSpec :: Expectations :: ExpectationNotMetError)./features/step_definitions/users/add_user.rb:23:in /^I should signin$/' features/users/add_user.feature:13:in
我应该签名'
但我登录了!为什么?从头开始测试身份验证系统的方法是什么?
1 回答
但我可以告诉你,你的问题是一个范围问题 . 你的is_admin?添加到cucumber的世界的方法无法访问应用程序控制器的@current_user变量 .