无法使用 webrat、Rails 和 Authlogic 测试注销

发布于 2024-10-04 06:22:59 字数 2991 浏览 3 评论 0原文

我正在使用 Rails 和 authlogic 进行登录。我正在尝试编写一个集成测试来使用 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 时,我最终没有登录链接,并且注销链接仍然存在 - 表明我仍然登录。

I'm using rails with authlogic for sign in. I'm trying to write an integration test for signing out with rspec and webrat.

The problem I'm seeing is that webrat seems to get different behavior than I do when I click through. So, I can log in and then log out just fine when I'm clicking through my browser, but webrat seems unable to log out. I diagnose this because I only show the log out link if you're logged in, but it's still being found by webrat after clicking log out.

Here's my test code

  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 

A few lines from my 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'

the links I'm looking for

 <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>

from user_sessions_controller

  def destroy 
    current_user_session.destroy
    flash[:notice] = "Logout successful!" 
    redirect_to root_url
  end 

from 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

Relevant gem versions

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)

So, in conclusion, when I log out manually, I go to the home page, and I have the log in link available to me with no log out link. When I go through webrat in the test above, I end up with no log in link, and the log out link is still there -- indicating that I'm still signed in.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2024-10-11 06:22:59

如果您使用 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 步骤就开始通过。

This will happen if you are using the cookie session store, rather than the database session store. See config/initializers/session_store.rb.

Yesterday, I began porting a project under development from 2.3.5 to 2.3.8. Under 2.3.5, all specs and features were green. After changing the Rails version to 2.3.8, several Cucumber steps started failing. The steps were related to an inability to sign out (exactly what you describe here) and to the flash[:notice] being lost. This project was created by copying files from another project into a clean-slate Rails project. In the process, the session migrations were copied, but session_store.rb was not updated to actually use the database.

Once I uncommented "ActionController::Base.session_store = :active_record_store" in session_store.rb, the Cucumber steps started passing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文