2017-10-25 4 views
0

私は以下のカピバラのテストケースを持っています。ログインページ認証のためのCapybaraテストケース

it "Testing login page with valid data" do 
    fill_in 'email', with: '[email protected]' 
    expect(page).to have_selector("input[value='[email protected]']")#Checking values are inserted in email field 

    fill_in 'password', with: 'Kiran.6565' 
    expect(page).to have_selector("input[value='Kiran.6565']")#Checking values are inserted in password field 

    click_button('submit') 

    expect(current_path).to eql(patient_detail_path(4)) 

end 

メールアドレスとパスワードのフィールドが、それはidフィールドの値をpatient_details_pathにリダイレクトする必要があります一致している一度私は、ログインページをチェックしています。上記のコードでは、私は指定された電子メールとパスワードが手動ログインのために正常に動作していますが、問題はテストケースです。期待される結果:別のページ(patient_details_path)にリダイレクトするが、ホームページ(/)に再度リダイレクトする必要があります。

Failures: 

1) Login Page Interface Test login page with valid data 
    Failure/Error: expect(current_path).to eql(patient_detail_path(4)) 

    expected: "/patient_details/4" 
     got: "/" 

    (compared using eql?) 
# ./spec/views/login_spec.rb:41:in `block (2 levels) in <top (required)>' 

Finished in 1.08 seconds (files took 2.13 seconds to load) 
14 examples, 1 failure 

私は別の解決策をstackoverflowから試しましたが、何も私のために働きません。以下は、さまざまなソリューションを試してみました。

#expect(current_path).to eql(patient_detail_path(4)) 
#expect(page).to have_current_path(patient_detail_path(4)) 

電子メールとパスワードが一致しないと、エラーが発生し、再度ログインページにリダイレクトされます。私のシナリオでは、電子メールとパスワードが有効であってもエラーを投げていました。テストケースに以下のコードを追加すると、テストケースに合格します。

#expect(page).to have_content "Invalid username/password combination" 

私はルビーとカピバラを初めて習っています。

+0

カピバラのテストではなく、何とかしたらどうなりますか?あなたは結果を期待していますか? – meta

+0

IDとパスワードを使用してユーザーを作成しているコードはどこにありますか?また、パスをテストするときには常に 'have_current_path'マッチャーを使用するべきです - あなたの現在のテストが不安定になる方法。また、これはビュー仕様ではないので、/ spec/views /にあるべきではありません。 –

+0

@metaもしログインすれば手動で動作します。 –

答えて

1

私はあなたが書くしようとしているテストは、一般的な推測だとあなたがしようとしている正確に何を推測する(タフな100%正しくない可能性があります

before :each do 
    @user = # Create the required user with whatever method you're using 
    @patient = # Create the required patient with whatever method you're using 
end 

it "Logs in with valid data" do 
    visit(patient_detail_path(@patient)) # gets redirected to the login path 
    fill_in 'email', with: '[email protected]' 
    fill_in 'password', with: 'Kiran.6565' 
    click_button('submit') 

    expect(page).to have_current_path(patient_detail_path(@patient)) 
end 

のようなものを記述する必要があります推測していますテストの半分を欠いて - 前のブロック - あなたの質問から)しかし、一般的な部分がそこにあるはずです。あなたがログインしていないので、実際には、指定された電子メールとパスワードで有効なユーザーを作成していないか、またはIDが4の患者を作成していないと思います(本当に頼りにすべきではありません特定のID番号を機能テストでテストします)。

また、指定されたパス/ URLをチェックするときには、テスト薄片を防ぐことができますので、あなたは常にhave_current_pathマッチャーを使用する必要があり、それがビューのテストではありませんので、それはspec/views/login_spec.rbにすべきではない、より適切にはspec/features/login_spec.rbでしょう。

0

Railsが新しいページを反映するためにURLを更新する前に、ドライバがURLを取得しているようです。

競合状態が発生する可能性があるため、ナビゲーションの変更を実行した後でURLにアサートするのは難しいです。私は提案します:

ユーザが正常にログインしたことを確認できる他の情報をアサートしてください。 待機オプションを使用してアサートしてください。

expect(page).to have_current_path(patient_detail_path(@patient), wait: 3)

関連する問題