これは、次の環境でのコントローラー仕様の Cookie コレクションの問題です。
- レール 3.1.0.rc4
- rspec 2.6.0
- rspec-rails 2.6.1
Factory ユーザーを作成し、Cookie を設定するサインイン メソッドを呼び出し、サインインしたユーザーがページにアクセスできるかどうかをテストする単純なコントローラー仕様があります。問題は、認証 Cookie が設定されてからコントローラーで「表示」アクションが呼び出されるまでの間、すべての Cookie が消えているように見えることです。
私のコードは、Rails dev サーバーのブラウザーで実行すると正常に動作します。仕様の実行中のさらに奇妙な動作は、cookie ハッシュを介して設定されたものはすべて消えますが、セッション ハッシュを介して設定されたものはすべて存続することです。rspec を使用するときに Cookie がどのように機能するかについて、何か不足しているだけですか?
仕様コード
it "should be successful" do
@user = Factory(:user)
test_sign_in @user
get :show, :id => @user
response.should be_success
end
サインインコード
def sign_in(user, opts = {})
if opts[:remember] == true
cookies.permanent[:auth_token] = user.auth_token
else
cookies[:auth_token] = user.auth_token
end
session[:foo] = "bar"
cookies["blah"] = "asdf"
self.current_user = user
#there are two items in the cookies collection and one in the session now
end
Cookie[:auth_token] が nil であるため、get :show リクエストの認証チェックはここで失敗します
def current_user
#cookies collection is now empty but session collection still contains :foo = "bar"... why?
@current_user ||= User.find_by_auth_token(cookies[:auth_token]) if cookies[:auth_token]
end
これはバグですか?私が理解できないのは、ある種の意図された動作ですか?明らかなことを見過ごしているだけですか?