没有使用webrat,Rails和Authlogic测试注销

时间:2010-11-25 18:13:49

标签: ruby-on-rails rspec authlogic integration-testing webrat

我正在使用带有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

相关宝石版本

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

1 个答案:

答案 0 :(得分:1)

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

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

在session_store.rb中取消注释“ActionController :: Base.session_store =:active_record_store”后,Cucumber步骤开始传递。