我无法让我的注销测试工作,但它可以在浏览器中运行。我看不出有什么不对劲。
test "logout user" do
chloe = login(:starrychloe, 'passpasspass')
chloe.delete_via_redirect "/users/logout"
chloe.assert_equal '/', chloe.path
chloe.assert_equal "Logged out!", chloe.flash.notice, chloe.flash.to_a
chloe.assert_select '.button' do |elem|
puts elem
end
chloe.assert_select '.button', 'Login'
这是错误。它仍然认为它已登录,即使会话被销毁且user_id
不再在会话中:
Started DELETE "/users/logout" for 127.0.0.1 at 2014-05-31 23:28:17 -0400
Processing by UsersController#logout as HTML
******************** {"session_id"=>"174289eb62bf8679db7f7b04e28c0822", "invalidLogin"=>0, "user_id"=>1, "flash"=>{"discard"=>["notice"], "flashes"=>{"notice"=>"Logged in! Last seen from 172.196.66.44."}}, "_csrf_token"=>"iKYW4LJvZU/1jrw7P6nnZho2ePms3uDde4EH/y5bRjM="}
******************** {"session_id"=>"7dbc9379256566a5bb642c91a9301501"}
Redirected to http://www.example.com:443/
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2014-05-31 23:28:17 -0400
Completed 200 OK in 8ms (Views: 6.8ms | ActiveRecord: 0.0ms)
<div class="button"><a href="/">All</a></div>
<div class="button"><a data-method="delete" href="/users/logout" rel="nofollow">Logout</a></div>
F
1) Failure:
UserFlowsTest#test_logout_user [test/integration/user_flows_test.rb:43]:
<Login> expected but was
<All>..
Expected 0 to be >= 1.
1 runs, 0 assertions, 1 failures, 0 errors, 0 skips
这是控制器方法
def logout
puts "******************** #{session.to_hash}"
reset_session
puts "******************** #{session.to_hash}"
redirect_to root_url, :notice => "Logged out!"
我最近将其从method: :post
更改为method: :delete
。我也试过redirect_to :root
,但这没有帮助。
答案 0 :(得分:0)
好的,我在Rails中发现了另一个错误。我添加了puts chloe.response.body
并看到它正在打印正确的未登录页面,但由于某种原因,assert_select仍在使用之前的页面!我通过从response.body创建一个新的HTML :: Document元素并直接选择该文本来解决问题。
puts chloe.response.body
doc = HTML::Document.new chloe.response.body
chloe.assert_select doc.root, '.button' do |elem|
puts elem
end
chloe.assert_select doc.root, '.button', 'Login'
现在它正确列出了新用户将看到的链接
<div class="button"><a href="/">All</a></div>
<div class="button"><a data-no-turbolink="true" href="/users/login">Login</a></div>
<div class="button"><a data-no-turbolink="true" href="/users/new">Register</a></div>