8

Rails 統合テストを書きたい (とActionDispatch::IntegrationTest)。認証にはdeviseを、テストモデルには機械工を使用しています。正常にサインインできません。

簡単な例を次に示します。

class UserFlowsTest < ActionDispatch::IntegrationTest
  setup do
    User.make
  end
  test "sign in to the site" 
    # sign in
    post_via_redirect 'users/sign_in', :email => 'foo@bar.com', :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: "foo@bar.com", 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 {"foo@bar.com"}
  password {"qwerty"}
end
4

5 に答える 5

10

機能しない理由は、devise がフォーム フィールド名を次のように作成するためです。

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

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

post_via_redirect 'users/sign_in', 'user[email]' => 'foo@bar.com', 'user[password]' => 'qwerty' 
于 2013-05-07T21:15:23.647 に答える
8

まず、デバイスを使用して、ユーザーを「確認」する必要がある場合があります。

あなたはこのようなことをすることができます:

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

Machinistを使用しない例を次に示します(ただし、ユーザー作成コードの部分を上記の部分に置き換える必要があります)。

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に:

require 'test_integration_helper'

class UsersIntegrationTest < ActionController::IntegrationTest

  test "sign in and then sign out" do 
    #this helper method is into the test_integration_helper file                   
    sign_in_as("lolz@gmail.com", "monkey")         
    assert page.has_content?('Signed in successfully'), "Signed in successfully"

    sign_out         
    assert page.has_content?('Signed out successfully'), "Signed out successfully" 
  end
end
于 2010-12-03T20:34:18.610 に答える
3

を使用しているため、モジュールをActionDispatch::IntegrationTest含めて代わりにメソッドを使用できます(ユーザーを渡すことができます):Devise::Test::IntegrationHelperssign_in

class UserFlowsTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers

  test "sign in to the site" do
    sign_in users(:one)
  end
end
于 2018-10-19T15:11:07.347 に答える
2

Deviseの本にはサンプルの統合テストはありませんが、Webrat / Capybaraがインストールされている場合は、Cucumberの章のステップ定義のコードも機能すると思います。

@user = User.create!(:email => "email@email.com",  
            :password => "password",  
            :password_confirmation => "password")
visit "login"  
fill_in("user_email", :with => @user.email)  
fill_in("user_password", :with => "password") 
click_button("Sign In")
于 2010-12-03T20:00:19.507 に答える
1

他の誰かが同様の問題を抱えている場合...

私は同様の状況 (Authlogic からの移行) にあり、まったく同じ問題を抱えていました。この問題は、devise.rb 初期化子にありました。デフォルトでは、パスワードの暗号化/復号化中に 1 つのストレッチのみを使用するようにテスト環境をセットアップします。正直なところ、ストレッチが何であるかはわかりませんが、Devise が古い Authlogic で暗号化されたパスワードを処理するには、ストレッチが 20 である必要があります。私のテストでは、もともと Authlogic によって暗号化されたパスワードを持っていたユーザーにサインインしようとしていたので、 Devise は、テストでも 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
于 2012-08-29T16:48:53.390 に答える