Cookie 不会保留在 Rails 3.1 上的 Rspec 中

发布于 2024-11-26 10:01:44 字数 1258 浏览 3 评论 0原文

这是以下环境中控制器规范中的 cookies 集合的问题:

  • rails 3.1.0.rc4
  • rspec 2.6.0
  • rspec-rails 2.6.1

我有一个简单的控制器规范,它创建一个工厂用户,调用登录方法它设置一个 cookie,然后测试登录用户是否可以访问页面。问题是,在设置身份验证 cookie 和在我的控制器上调用“显示”操作之间,所有 cookie 似乎都消失了。

我的代码在 Rails 开发服务器上的浏览器中运行时运行良好。运行规范时更奇怪的行为是,通过 cookies 哈希设置的所有内容都会消失,但通过会话哈希设置的所有内容仍然存在。我是否只是错过了使用 rspec 时 cookie 的工作原理?

规范代码

it "should be successful" do
  @user = Factory(:user)
  test_sign_in @user
  get :show, :id => @user
  response.should be_success
end

登录代码

def sign_in(user, opts = {})
  if opts[:remember] == true
    cookies.permanent[:auth_token] = user.auth_token
  else
    cookies[:auth_token] = user.auth_token
  end
  session[:foo] = "bar"
  cookies["blah"] = "asdf"
  self.current_user = user
  #there are two items in the cookies collection and one in the session now
end

对 get 的身份验证检查:show 请求在此失败,因为 cookies[:auth_token] 为 nil

def current_user
 #cookies collection is now empty but session collection still contains :foo = "bar"... why?
 @current_user ||= User.find_by_auth_token(cookies[:auth_token]) if cookies[:auth_token]
end

这是一个错误吗?这是我不理解的某种预期行为吗?我只是忽略了一些显而易见的事情吗?

This is a problem with the cookies collection in a controller spec in the following environment:

  • rails 3.1.0.rc4
  • rspec 2.6.0
  • rspec-rails 2.6.1

I have a simple controller spec that creates a Factory user, calls a sign in method which sets a cookie, and then tests to see if the signed in user may access a page. The problem is that all cookies seem to disappear between the authentication cookie being set and the "show" action being called on my controller.

My code works fine when run in a browser on the rails dev server. The even stranger behavior while running the spec is that everything set though the cookies hash disappears but everything set through the session hash persists. Am I just missing something about how cookies work when using rspec?

Spec code

it "should be successful" do
  @user = Factory(:user)
  test_sign_in @user
  get :show, :id => @user
  response.should be_success
end

Sign in code

def sign_in(user, opts = {})
  if opts[:remember] == true
    cookies.permanent[:auth_token] = user.auth_token
  else
    cookies[:auth_token] = user.auth_token
  end
  session[:foo] = "bar"
  cookies["blah"] = "asdf"
  self.current_user = user
  #there are two items in the cookies collection and one in the session now
end

Authentication check on the get :show request fails here because cookies[:auth_token] is nil

def current_user
 #cookies collection is now empty but session collection still contains :foo = "bar"... why?
 @current_user ||= User.find_by_auth_token(cookies[:auth_token]) if cookies[:auth_token]
end

Is this a bug? Is it some sort of intended behavior that I don't understand? Am I just looking past something obvious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓味的萝莉 2024-12-03 10:01:44

这就是我解决这个问题的方法:

def sign_in(user)
 cookies.permanent.signed[:remember_token] = [user.id, user.salt]
 current_user = user
 @current_user = user
end

def sign_out
 current_user = nil
 @current_user = nil
 cookies.delete(:remember_token)
end

def signed_in?
 return !current_user.nil?
end

出于某种原因,您必须设置 @current_user 和 current_user 才能使其在 Rspec 中工作。我不知道为什么,但这让我完全抓狂,因为它在浏览器中运行良好。

This is how I solved this:

def sign_in(user)
 cookies.permanent.signed[:remember_token] = [user.id, user.salt]
 current_user = user
 @current_user = user
end

def sign_out
 current_user = nil
 @current_user = nil
 cookies.delete(:remember_token)
end

def signed_in?
 return !current_user.nil?
end

For some reason you have to set both @current_user and current_user for it to work in Rspec. I'm not sure why but it drove me completely nuts since it was working fine in the browser.

傾旎 2024-12-03 10:01:44

遇到了同样的问题。尝试在测试中使用 @request.cookie_jar

Ran into the same problem. Try using @request.cookie_jar from your tests.

巷雨优美回忆 2024-12-03 10:01:44

上面的额外行不是必需的。我发现只需调用 self.current_user = user setter 方法就可以自动更新实例变量。由于某种原因,在没有 self 的情况下调用它不会调用 setter 方法。

这是我的代码,没有额外的行:

def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  self.current_user = user
end

def sign_out
  cookies.delete(:remember_token)
  self.current_user = nil
end

def current_user=(user)
  @current_user = user
end

我仍然不知道为什么,也许是 Rspec bug

The extra lines above are not necessary. I found that simply calling the self.current_user = user setter method can automatically update the instance variable. For some reason, calling it without self doesn't call the setter method.

Here's my code without the extra lines:

def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  self.current_user = user
end

def sign_out
  cookies.delete(:remember_token)
  self.current_user = nil
end

def current_user=(user)
  @current_user = user
end

I still don't know why, maybe an Rspec bug

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文