私はこれがうまくいかない理由を理解しようとしています。
User、Foo、Barの3つのモデルを考えてみましょう。バーを作成するには、ユーザーは最初にfooオブジェクトを作成して検証する必要があります。
Class User #snip!
has_many :foos
has_many :bars
Class Foo #snip!
belongs_to :user
has_many :bars
Class Bar #snip!
belongs_to :user
belongs_to :foo
ユーザーが有効なfooを持たずに新しいバーを作成しようとすると、Fooの「新しい」アクションにリダイレクトされる機能テストを機能させようとしています。
リダイレクトシナリオに問題はありません。ただし、有効なFooオブジェクトを使用してユーザーを設定し、バーの「新しい」アクションを取得しようとすると、Fooコントローラーの「新しい」アクションにリダイレクトされます。それでも、ユーザーがFooを持っていることを認識しません。
これが私のコントローラーです:
 class BarsControllerTest < ActionController::TestCase
  setup :activate_authlogic
  def setup
    @request.env['HTTPS'] = nil
    @user = Factory.build(:user)
    @foo = Factory.build(:foo, :user => @user)
  end
   test "should get new when user has a valid foo" do
     @request.env['HTTPS'] = 'on'
     UserSession.create(@user)
     get :new
     assert_response :success
  end
これは、アプリケーションコントローラーにあるリダイレクト関数であり、バーコントローラーで呼び出されます。
  def foo_required
    if current_user && @current_user.foos.valid.empty? && @current_user.foos.empty?
    flash[:notice] = "You must have a verified foo in order to create a Bar!"
    redirect_to new_foo_path
    elsif current_user && @current_user.foos.valid.empty?
    flash[:notice] = "You must verify your foos in order to create a Bar!"
    redirect_to foos_path
    end
 end
これがFooFactoryです。
Factory.define :foo do |f|
   #attributes
   f.valid true
   f.association :user
end
代わりに、「https://test.host:80 / foos / new」にリダイレクトされます。コントローラーは、ユーザーがFooを持っていることを認識しません...
セッションは有効なので、これは工場の問題のようですが、それが何であるかはわかりません。