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 に答える