我正在使用rails with authlogic進行登錄。我正在嘗試使用rspec和webrat進行註銷的集成測試。未能通過webrat測試註銷,Rails with Authlogic
我看到的問題是,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'
幾行,我從user_sessions_controller尋找
<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>
鏈接
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時,我最終沒有登錄鏈接,並且註銷鏈接仍然存在 - 表示我仍然登錄。
我認爲bug是在`click_link /註銷/ i`行。也許你應該使用`click_link'註銷'`而不是? – tjeden 2010-11-25 20:59:45
我試過了。與描述相同的行爲。 – 2010-11-25 21:13:12