Cookie 不会保留在 Rails 3.1 上的 Rspec 中
这是以下环境中控制器规范中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我解决这个问题的方法:
出于某种原因,您必须设置 @current_user 和 current_user 才能使其在 Rspec 中工作。我不知道为什么,但这让我完全抓狂,因为它在浏览器中运行良好。
This is how I solved this:
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.
遇到了同样的问题。尝试在测试中使用
@request.cookie_jar
。Ran into the same problem. Try using
@request.cookie_jar
from your tests.上面的额外行不是必需的。我发现只需调用 self.current_user = user setter 方法就可以自动更新实例变量。由于某种原因,在没有 self 的情况下调用它不会调用 setter 方法。
这是我的代码,没有额外的行:
我仍然不知道为什么,也许是 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:
I still don't know why, maybe an Rspec bug