0

我正在使用带有 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 时,我最终没有登录链接,并且注销链接仍然存在 - 表明我仍然在登录。

4

1 回答 1

1

如果您使用的是 cookie 会话存储,而不是数据库会话存储,则会发生这种情况。请参阅 config/initializers/session_store.rb。

昨天,我开始将一个正在开发的项目从 2.3.5 移植到 2.3.8。在 2.3.5 下,所有规格和功能都是绿色的。将 Rails 版本更改为 2.3.8 后,几个 Cucumber 步骤开始失败。这些步骤与无法退出(正是您在此处描述的内容)和 flash[:notice] 丢失有关。这个项目是通过将文件从另一个项目复制到一个全新的 Rails 项目中创建的。在此过程中,会话迁移被复制,但 session_store.rb 没有更新为实际使用数据库。

一旦我在 session_store.rb 中取消注释“ActionController::Base.session_store = :active_record_store”,Cucumber 步骤就开始通过了。

于 2010-12-22T19:46:09.417 回答