質問

私は一生、Authlogicがこの統合テストでログインしない理由を理解していません。このコードを使用した機能テストでAuthlogicがログインしていても問題はありませんでした。 authlogic rdocs( http://tinyurl.com/mb2fp2 )によると、ログイン状態のシミュレーションは機能的にも&統合テストなので、かなり混乱しています。どんな助けも大歓迎です!

class TipsController < ApplicationController
  before_filter :require_user,  :only => [:destroy, :undelete]
  def destroy
    @tip = Tip.find(params[:id])

    if can_delete?(@tip)

      @tip.destroy

      set_flash("good", "Tip deleted. <a href=\"#{undelete_tip_url(@tip.id)}\">Undo?</a>")
      respond_to do |format|
        format.html { redirect_to city_path(@tip.city)} 
      end
    else
      set_flash("bad", "Seems like you can't delete this tip, sorry.")
      respond_to do |format|
        format.html { render :action => "show", :id => @tip} 
      end
    end
  end
end


class DeleteTipAndRender < ActionController::IntegrationTest
  context "log user in" do
    setup do
      @user = create_user
      @tip = create_tip
    end

    context "delete tip" do
      setup do
        activate_authlogic
        UserSession.create(@user)
        @us = UserSession.find
        post "/tips/destroy", :id => @tip.id
      end

      should_redirect_to("city_path(@tip.city)"){city_path(@tip.city)} 
    end
  end
end
役に立ちましたか?

解決

ログイン資格情報のハッシュを取得する user_sessions_controller create メソッドのコードに基づいて、統合テストで次のように動作させることができました。

UserSession.create(:email => 'someone@example.com', :password => 'password')

ではなく:

UserSession.create(@user)

他のヒント

また、ShouldaでAuthlogicを使用しています(ただし、factory_girlは上にあります)。

私の機能テストは次のようになります:

require 'test_helper'

class LoansControllerTest < ActionController::TestCase
[...]

  context "as a signed-in user, with an active loan" do
    setup do
      @user = Factory(:user)
      @user_session = UserSession.create(@user)
      @loan = Factory(:loan, :ownership => Factory(:ownership, :user => @user))
    end

    context "on GET to :index" do
      setup do
        get :index
      end

      should_respond_with_success
    end
  end
end

実際には、有効なユーザーをUserSessionに渡すことができます。これはrdocにもあります。 各コントローラーテストでactivate_authlogicを呼び出すことも避けてください:

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'

class ActiveSupport::TestCase
  [...]
  # Add more helper methods to be used by all tests here...
  include Authlogic::TestCase

  def setup
    activate_authlogic
  end
end

統合テストでは、投稿からログインする必要があることがわかりました:

setup do
  post 'user_session', :user_session => {:email => 'someone@example.com', :password => 'password'}
end

これはセッションを正しく設定しますが、上記の方法は機能テストでのみ機能します。

ええ、私は今週、rspecを使用して、機能仕様で UserSession.create(@user)を使用してログをシミュレートすることを発見しました。しかし、統合仕様でそれを試しても機能しません。統合仕様からログインするには、FacebookやOpenIDのログインなどで明らかに問題になるフォーム(webratを使用)を操作する必要がありました。

私は受け入れられた答えでそれを機能させることができなかったが、近かった。関数の最後に感嘆符を追加する必要がありました:

UserSession.create!(@user)

Ruby v3.2.18およびAuthlogic v3.4.2で動作しています。私を正しい方向に向けてくれてありがとう。

私はCucumberとJamesのソリューションを次の修正とともに使用しています。

https://webrat.lighthouseapp.com/projects/10503/tickets/383-reset_session-and-webrat-dont-play-nicely-with-one-another

この投稿をGoogleとGreater Justiceで見つける人のために-ユーザーモデルで persitence_token 属性を設定する必要があります。例えば。 @ user.reset_persistence_token!を呼び出すと、すべてが機能し始めます。 :)

同じ問題があり、手動でログインすることで修正できました(他の人がすでに答えているように)

さらに、 cookieドメインを修正する必要がありました:

Railsはテストに www.example.com を使用します。これは、 application.rb でCookieドメインを別の値に設定するためです( config。 cookie_domain )ログイン後に作成されたセッションCookieは、後続のリクエストからアクセスできませんでした。

正しいcookieドメインを設定した後、すべてが再び機能しました。

rdoc をご覧ください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top