私はMichael HartlのRoRチュートリアルを実行しようとしていますが、それは3回目ですが、チュートリアルのホーム認証ではなくDeviseを実装しようとしています。あなたが想像することができるように、これは少し痛みがあります。主に、RoRチュートリアルのテスト駆動開発がDeviseとうまく混ざっていないためです。私の現在のハングアップは、成功したログインを正常にテストすることができないということです。私はlocalhost上のウェブサイトを持っているので、私はログインが動作することを知っているが、私のテストは何らかの理由で失敗している。ここでは、統合テスト抜粋ですDeviseで成功したログインをテストする
def setup
@user = users(:michael)
end
...
test "login with valid information" do
get user_session_path
assert_equal 200, status
post user_session_path, 'user[email]' => @user.email, 'user[password]' => "password"
follow_redirect!
assert_equal 200, status
assert_select "a[href=?]", user_session_path, count: 0 # THIS LINE FAILS
assert_select "a[href=?]", destroy_user_session_path
assert_select "a[href=?]", user_path(@user)
end
それは、ユーザーを取得していないテストのように見えるので、私が得るエラーは、
FAIL["test_login_with_valid_information", UsersLoginTest, 2016-06-19 11:06:18 -0400]
test_login_with_valid_information#UsersLoginTest (1466348778.61s)
Expected exactly 0 elements matching "a[href="https://stackoverflow.com/users/sign_in"]", found 1..
Expected: 0
Actual: 1
Sign in
からAccount
のトップスイッチでログインバーアップしていますセットアップブロックが正常にサインインしました。もう1つ、おそらく関連性のない問題は、別のテストでは、私はpost user_session_path...
と同じ正確なログイン方法を試してからassert_not current_user.nil?
でヌルではないことを確認しますが、エラーが発生します。
私はroutes.rbファイルに正しい行がdevise_for :users
であることを確認しました。私は確かにそれはcurrent_user
にアクセスすることができません。何とか私はDeviseのリソースにアクセスできる統合テストを持っていないからです。それらは私の2つの問題です!どんな助けでも大歓迎です。
編集:私は代わりに、ログインをテストする従来の方法の
post_via_redirect user_session_path, 'user[email]' => @user.email, 'user[password]' => @user.password
を試してみましたが、私は次のエラー
ERROR["test_login_with_valid_information", UsersLoginTest, 2016-06-28 20:40:59 -0400]
test_login_with_valid_information#UsersLoginTest (1467160859.16s)
RuntimeError: RuntimeError: not a redirect! 200 OK
test/integration/users_login_test.rb:36:in `block in <class:UsersLoginTest>'
test/integration/users_login_test.rb:36:in `block in <class:UsersLoginTest>'
ERROR["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 2016-06-28 20:40:59 -0400]
test_login_with_valid_information_followed_by_logout#UsersLoginTest (1467160859.18s)
NameError: NameError: undefined local variable or method `current_user' for #<UsersLoginTest:0x0055d0b9e0df50>
test/integration/users_login_test.rb:49:in `block in <class:UsersLoginTest>'
test/integration/users_login_test.rb:49:in `block in <class:UsersLoginTest>'
編集2を得る:
私はジェミマの提案治具を使用テストをやり直してください。テストログをチェックインすると、私は見つけました
-------------------------------------------------
UsersLoginTest: test_login_with_valid_information
-------------------------------------------------
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 762146111]]
Started GET "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at 2016-07-01 18:46:41 -0400
Processing by Devise::SessionsController#new as HTML
Rendered devise/shared/_links.html.erb (0.4ms)
Rendered devise/sessions/new.html.erb within layouts/application (1.6ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (0.3ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 4ms (Views: 3.4ms | ActiveRecord: 0.0ms)
Started POST "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at 2016-07-01 18:46:41 -0400
Processing by Devise::SessionsController#create as HTML
Parameters: {"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}}
Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)
Processing by Devise::SessionsController#new as HTML
Parameters: {"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}}
Rendered devise/shared/_links.html.erb (0.5ms)
Rendered devise/sessions/new.html.erb within layouts/application (2.1ms)
Rendered layouts/_shim.html.erb (0.1ms)
Rendered layouts/_header.html.erb (0.3ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 6ms (Views: 5.3ms | ActiveRecord: 0.0ms)
(0.1ms) rollback transaction
(0.1ms) begin transaction
それは許可されていないようですね?また、私はinclude Devise::Test::IntegrationHelpers
をユーザーのログインテストの最上位に置こうとしましたが、エラーNameError: uninitialized constant Devise::Test
がスローされます。
念のために、あなたは[記事](https://github.com/plataformatec/devise/wiki/How-To:-Test-controllersことを読みました-with-Rails-3-and-4-%28and-RSpec%29)を参照してください。あなたがユニットをテストするたびにdeviseコントローラを使う必要があるたびに、どのマッピングを使うべきかをDeviseに伝える必要があります。 ActionController :: TestCaseとspec/controllersはルータをバイパスしているので、route.rbへの参照はテストでは機能しません。 –
'current_user'ヘルパーに関しては、Devise gemによってApplicationControllerに追加されています。デフォルトでは、テストでは使用できません。 [here](https://github.com/plataformatec/devise/blob/master/lib/devise/test_helpers.rb)を参照してください。 - ''コントローラテストの場合は、代わりにDevise :: Test :: ControllerHelpers'を含めてください.'' –
最後に、問題の原因を深く調査する必要があります。出力にsign_inリンクが残っていれば、ここでは役に立ちません。投稿に何が起こっているのか、何がうまくいかないのかをデバッグする方法を見つけてください。いくつかのDeviseエラー。 –