LoginSignup
2
1

More than 3 years have passed since last update.

結合テストコードで出会ったエラー

Last updated at Posted at 2021-02-12

はじめまして、今回が初投稿です。

現在オリジナルアプリを作成していて、結合テストコードを行っています。

学習期間中結合テストコードの学習をしましたが、作成するアプリに記述するのは初めてなので

手こずってしまいました。

忘れないためにも、投稿したいと思います。

手こずったのは、新規登録のユーザー情報を入力するテストコードです。

初めに記述したコードがこちらです。

fill_in "メールアドレス", with: @user.email
fill_in "パスワード(半角英数混合6文字以上)", with: @user.password
fill_in "パスワード再入力", with: @user.password_confirmation
fill_in "プロフィール写真", with: @user.image

これは、新規投稿フォームにFactoryBotで生成した、情報を入力している記述です。
こちらを記述しターミナルから、

bundle exec rspec spec/system/users_spec.rb

を行ったところ

 1) Users 新規登録 新規登録できる時 正しい情報が登録できれば新規登録ができてトップページに遷移する
  Failure/Error: fill_in "パスワード(半角英数混合6文字以上)", with: @user.password

  Capybara::Ambiguous:
  Ambiguous match, found 2 elements matching visible field "パスワード(半角英数混合6文字以上)" that is not disabled

こちらのエラーが現れました。
これは、いろんな要素が重なってしましどれかわからない!!
みたいなエラーとのことでした。
なので、最初のものを選んであげるようにすれば、解決できるとのことでした。

fill_in "メールアドレス", with: @user.email
fill_in "パスワード(半角英数混合6文字以上)",match: :first, with: @user.password
fill_in "パスワード再入力", match: :first, with: @user.password_confirmation
fill_in "プロフィール写真", with: @user.image

最初のものを選んで上げるようにするには、match: :firstをつけてあげればいいとのことでした。

これで、解決!!
と思ったのですがその後に

Capybara::ElementNotFound:
Unable to find field "プロフィール写真" that is not disabled

とエラーが現れました。
要するに、写真の添付の仕方の記述が違ったようです。
私は、FactoryBotに記述した

after(:build) do |user|
        user.image.attach(io: File.open('public/images/test_image.png'), filename: 'test_image.png')
end

この記述を使えると思ったのですが、そうではなかったそうです。

fill_in "メールアドレス", with: @user.email
        fill_in "パスワード(半角英数混合6文字以上)",match: :first, with: @user.password
        fill_in "パスワード再入力", match: :first, with: @user.password_confirmation
        attach_file "user[image]", "public/images/test_image.png"

この記述に直したところ上手くいきました。

以上が今回手こずってしまったエラーになります。
初投稿で色々と不手際があるかもしれませんが、何かございましたらコメントお願いいたします。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1