我有一个带有以下步骤的 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 的session
对象中,然后 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
正如您在第三个请求中看到的那样,会话对象是空的(即使我们知道它在最后一个操作中已正确设置)。似乎会话数据不会在重定向后的请求中保留,但我不知道为什么(我已经花了几个小时!)。有任何想法吗?