Cookie不会持久存储在rails 3.1上的Rspec中 [英] Cookies do not persist in Rspec on rails 3.1

查看:155
本文介绍了Cookie不会持久存储在rails 3.1上的Rspec中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在以下环境中控制器规格中的cookie集合的问题:




  • rails 3.1.0.rc4

  • rspec 2.6.0

  • rspec-rails 2.6.1



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



我的代码运行在浏览器上的rails dev服务器。在运行规范时,甚至陌生人的行为是,尽管cookie哈希消失了,但通过会话哈希设置的一切仍然存在。



指定代码

  it应该成功do 
@user = Factory(:user)
test_sign_in @user
get:show,:id => @user
response.should be_success
end

登录代码

  def sign_in(user,opts = {})
如果opts [:remember] == true
永久性[:auth_token] = user.auth_token
else
cookie [:auth_token] = user.auth_token
end
session [:foo] =bar
[blah] =asdf
self.current_user = user
在cookie集合中有两个项目,现在在会话中有一个
end

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


$ b $ #cookies集合现在为空,但会话集合仍然包含:foo =bar...为什么?
@current_user || = User.find_by_auth_token(cookies [:auth_token])if cookies [:auth_token]
end

这是一个错误吗?是不是我不明白的某种意图的行为?

解决方案

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

  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
cookie.delete(:remember_token)
end

def signed_in?
return!current_user.nil?
end

由于某种原因,你必须同时设置@current_user和current_user才能工作在Rspec。我不知道为什么,但它驱动我完全坚果,因为它在浏览器工作正常。


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?

解决方案

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.

这篇关于Cookie不会持久存储在rails 3.1上的Rspec中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆