开发者

Failing to test sign out with webrat, Rails with Authlogic

开发者 https://www.devze.com 2023-01-27 12:32 出处:网络
I\'m using rails with authlogic for sign in.I\'m trying to write an integration test for signing out with rspec and 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 => '开发者_JS百科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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消