2010-11-27 24 views
6

レール統合テスト(ActionDispatch::IntegrationTest)を書きたいと思っています。私はテストモデルのための認証と機械工にdeviseを使用しています。私は正常にサインインすることはできませんdevises gemを使ったRails統合テスト

をここで簡単な例である:ここで

class UserFlowsTest < ActionDispatch::IntegrationTest 
    setup do 
    User.make 
    end 
    test "sign in to the site" 
    # sign in 
    post_via_redirect 'users/sign_in', :email => '[email protected]', :password => 'qwerty'  
    p flash 
    p User.all 
end 

は、デバッグ出力である:ここで

Loaded suite test/integration/user_flows_test 
Started 
{:alert=>"Invalid email or password."} 
[#<User id: 980190962, email: "", encrypted_password: "", password_salt: "", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2010-11-27 16:44:10", updated_at: "2010-11-27 16:44:10">, #<User id: 980190963, email: "[email protected]", encrypted_password: "$2a$10$vYSpjIfAd.Irl6eFvhJL0uAwp4qniv5gVl4O.Hnw/BUR...", password_salt: "$2a$10$vYSpjIfAd.Irl6eFvhJL0u", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2010-11-27 17:09:13", updated_at: "2010-11-27 17:09:13">] 
"/unauthenticated" 
F 

は私blueprints.rbです:

require 'machinist/active_record' 
require 'sham' 

User.blueprint do 
    email {"[email protected]"} 
    password {"qwerty"} 
end 
+0

私は最近、Rails Test Prescriptions(http://pragprog.com/titles/nrtest/rails-test-prescriptions)の読解を開始しました。かなり早い段階で、Noelは彼が認証のためにDeviseを使用すると述べています。私はgithub.comに彼のサンプルアプリケーション(https://github.com/noelrappin/huddle)を見て、彼がこの問題を解決しているかどうかを調べました。彼は統合テストを特徴としませんが、参考のためにここに掲載しています。 – hiwaylon

+0

また、私のアプリケーションで認証にAuthLogic(https://github.com/binarylogic/authlogic)を使用するように切り替えました。私は以前にそれを使用していたし、Deviseよりはるかに良い。 – hiwaylon

答えて

2

私はDeviseの本でサンプル統合テストを行っていませんが、私はCucumbeのステップ定義のコードはあなたがWebrat/Capybaraをインストールしている場合は、この章もうまくいきます。

@user = User.create!(:email => "[email protected]", 
      :password => "password", 
      :password_confirmation => "password") 
visit "login" 
fill_in("user_email", :with => @user.email) 
fill_in("user_password", :with => "password") 
click_button("Sign In") 
+0

お返事ありがとうございます。私はそのプロジェクトをもう一度掘り起こし、これがどのように機能するのかを見なければならないでしょう。今は+1。また、良い読んでいただきありがとうございます:) – hiwaylon

8

まず第一に、開発者は「確認」する必要があります。

あなたはこのような何か行うことができます:ここでは

user = User.make! 
user.confirmed_at = Time.now 
user.save! 

はマシニストなしの一例である(しかし、あなただけの上の部分で、ユーザー作成コードの部分を交換する必要があり):test_integration_helperに

require "test_helper" 
require "capybara/rails" 

    module ActionController 
     class IntegrationTest 
     include Capybara 

     def sign_in_as(user, password) 
      user = User.create(:password => password, :password_confirmation => password, :email => user) 
      user.confirmed_at = Time.now 
      user.save! 
      visit '/' 
      click_link_or_button('Log in') 
      fill_in 'Email', :with => user.email 
      fill_in 'Password', :with => password 
      click_link_or_button('Sign in') 
      user  
     end 
     def sign_out 
      click_link_or_button('Log Out') 
     end 
     end 
    end 

そして、あなたのintegration_testへ:

誰にも同様の問題がある場合は
+0

応答ありがとうございます。私はそのプロジェクトをもう一度掘り起こし、これがどのように機能するのかを見なければならないでしょう。今は+1。 – hiwaylon

1

...

私は似たような状況(Authlogicからの移行)にあった、そしてこれと同じ問題を抱えていました。問題はdevise.rbイニシャライザにありました。デフォルトでは、パスワードの暗号化/復号化中に1回だけ使用するようにテスト環境を設定します。私は正直なところストレッチが分かりませんが、古いAuthlogicの暗号化されたパスワードで動作させるDeviseの場合、20のストレッチが必要です。私のテストでAuthlogicが元々パスワードを暗号化していたユーザーにサインインしようとしていたので、デビーズはまた、テストで20のストレッチを使用する必要があります。私は以下のように設定を変更:

# Limiting the stretches to just one in testing will increase the performance of 
# your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use 
# a value less than 10 in other environments. 
- config.stretches = Rails.env.test? ? 1 : 20 
+ config.stretches = 20 
10

を、それが動作しない理由は工夫が

'user[email]' 
'user[password]' 

としてフォームのフィールド名を作成し、post_via_redirectが引数としてそれらの名前を期待しています。したがって、次のステートメントはログインに成功します。

post_via_redirect 'users/sign_in', 'user[email]' => '[email protected]', 'user[password]' => 'qwerty' 
+0

私の場合、JSONの統合テストであるpost_via_redirect 'users/sign_in'だったので、私の場合は、{email: "[email protected]"、password: "qwerty"}、format: jsonは働いた。 – kangkyu