【发布时间】:2012-04-30 01:31:49
【问题描述】:
我有一个包含以下步骤的 Cucumber 场景:
Given /^I have logged in$/ do
visit root_url
fill_in 'username', with: 'testuser'
fill_in 'password', with: 'testpass'
click_button 'Log In'
end
如果我运行我的场景并跟踪我的 Rails 日志,我可以确认 1) 登录页面加载,2) 提交登录详细信息时,它们被接受并且所需的用户信息正确存储在 Rails 的 @ 987654322@ 对象,然后 3) 会话数据在 302 重定向到登录后页面后丢失(因此是 403):
Started GET "/" for 127.0.0.1 at 2012-04-18 16:09:41 +0100
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (4.6ms)
Completed 200 OK in 9ms (Views: 8.3ms)
Started POST "/" for 127.0.0.1 at 2012-04-18 16:09:41 +0100
Processing by HomeController#index as HTML
Parameters: {"utf8"=>"✓", "is_submitted"=>"true", "username"=>"testuser", "password"=>"[FILTERED]", "commit"=>"Log In"}
DEBUG - login success
DEBUG - session: {"UserId"=>"19fd75c8-0e80-4832-94af-6a93ee74bf46", "Username"=>"testuser", "Password"=>"d68579bfdac2321d05f19042d8dbc49b9dd611c8", "Name"=>"Active User", "Active"=>true}
Redirected to http://www.example.com/app
Completed 302 Found in 3ms
Started GET "/app" for 127.0.0.1 at 2012-04-18 16:09:41 +0100
Processing by HomeController#app as HTML
DEBUG - session: {}
Redirected to http://www.example.com/
Completed 403 Forbidden in 1ms
正如您在第三个请求中看到的,会话对象是空的(即使我们知道它在最后一个操作中已正确设置)。似乎会话数据没有在重定向后的请求中保留,但我不知道为什么(我已经花了几个小时!)。有什么想法吗?
【问题讨论】:
-
您应该检查会话的存储位置。在 cookie 或数据库中。查看 session_store.rb 文件。
-
它正在使用 cookie 存储。 (我应该提到登录/会话的东西在“真正的浏览器”中工作得很好。)
标签: ruby ruby-on-rails-3 cucumber capybara