我正在使用带有 authlogic 的 rails 进行登录。我正在尝试编写一个集成测试以使用 rspec 和 webrat 注销。
我看到的问题是 webrat 的行为似乎与我点击时不同。所以,当我点击浏览器时,我可以登录然后注销就好了,但是 webrat 似乎无法注销。我对此进行了诊断,因为如果您已登录,我只会显示注销链接,但在单击注销后,webrat 仍会找到该链接。
这是我的测试代码
describe "when not signed in" do
it "should have a sign in link" do
visit root_path
response.should have_tag("a[href=?]", login_path, "Log In")
end
end
describe "when signed in" do
before :each do
@user = Factory(:user)
visit login_path
fill_in :user_session_email, :with => @user.email
fill_in :user_session_password, :with => @user.password
click_button
end
it "should have a log out button" do
visit root_path
response.should have_tag("a[href=?]", logout_path, "Log Out")
end
# This is the test that's failing
it "we should be able to log out" do
visit root_path
click_link /log out/i
response.should render_template('merchant_pages/home')
#this next line is the one that fails
#I've played around with this, and the log out link is still there
response.should have_tag("a[href=?]", login_path, "Log In")
end
end
我的 routes.rb 中的几行
map.resources :users
map.resources :user_sessions
map.login '/login', :controller => 'user_sessions', :action => 'new'
map.logout '/logout', :controller => 'user_sessions', :action => 'destroy'
我正在寻找的链接
<ul class="navigation round">
<li><%= link_to("Log In", login_path) unless current_user %></li>
<li><%= link_to("Log Out", logout_path) if current_user %></li>
</ul>
来自 user_sessions_controller
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_to root_url
end
从 application_controller
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
相关 gem 版本
authlogic (2.1.6)
rails (2.3.8)
rake (0.8.7)
rspec (1.3.0)
rspec-core (2.0.1)
rspec-expectations (2.0.1)
rspec-mocks (2.0.1)
rspec-rails (1.3.2)
webrat (0.7.2)
因此,总而言之,当我手动注销时,我会转到主页,并且我可以使用登录链接,但没有注销链接。当我在上面的测试中通过 webrat 时,我最终没有登录链接,并且注销链接仍然存在 - 表明我仍然在登录。