LoginSignup
0
0

More than 1 year has passed since last update.

【rails6】結合テストエラーの解決 `it` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).

Posted at

こんにちは!
今日も結合テストで犯した凡ミスについて共有していこうと思います

凡ミスばかりですみません…
でもこういった凡ミスで無駄な時間を過ごしてしまう方々が一人でも減ればなぁと信じて
是非読んでいってください!

では、まずは今日のエラーをご紹介

発生したエラー

 % bundle exec rspec spec/system/comments_spec.rb  # この結合テストを実行した際に

Failure/Error:
       it 'ログインしたユーザーはコメントできる(自分の投稿にコメントする場合)' do
         # 省略

       `it` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
       # ↑このエラーが発生しました↑

さて、このエラー文

`it` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).

を日本語訳してみましょう

it` は、サンプル内(例:`it` ブロック)や、サンプルのスコープ内で実行されるコンストラクト(例:`before`、`let` など)からは利用できません。サンプルグループ(例:`describe`や`context`ブロック)でのみ利用可能です。

みなさん英文を見た瞬間に「うわっ…」となるかもしれませんが、ここは慣れましょう
便利な翻訳サイトのリンクは下に貼りますが、できる限りご自身で読み解くことをお勧めします
※今はQiita投稿の為翻訳してます

では、原因となるコードを見てみましょう

comments_spec.rb
it '空欄のコメント送信はできない' do
  it 'ログインしたユーザーでも空欄コメントは送信できない' do
    # ログインし、root_pathにいることを確認する
    sign_in(@article1.user)
    expect(current_path).to eq root_path
    expect(page).to have_no_content('ログイン')
    # @article1の詳細ページに遷移する
    visit article_path(@article1)
    expect(page).to have_content(@article1.title)
    # コメント欄があることを確認、フォームに入力する
    expect(page).to have_selector(".send-comment-text")
    fill_in "コメントを入力", with: ''
    # コメントを送信しても、Commentモデルのカウントが上がらない事を確認する
    expect{find('input[name="commit"]').click}.to change { Comment.count }.by(0)
    # コメントがページこのページ内に存在していないか確認する
    expect(page).to have_no_content(@comment)
  end
end

何がいけないか、もうお分かりですよね??
itの中にitを書いてしまっている!!
という事です

「別のテストコードをコピペ活用する中で、不要なit文を削除し忘れた」ということですね

では、解決していきましょう

comments_spec.rb
it 'ログインしたユーザーでも空欄コメントは送信できない' do
  # ログインし、root_pathにいることを確認する
  sign_in(@article1.user)
  expect(current_path).to eq root_path
  expect(page).to have_no_content('ログイン')
  # @article1の詳細ページに遷移する
  visit article_path(@article1)
  expect(page).to have_content(@article1.title)
  # コメント欄があることを確認、フォームに入力する
  expect(page).to have_selector(".send-comment-text")
  fill_in "コメントを入力", with: ''
  # コメントを送信しても、Commentモデルのカウントが上がらない事を確認する
  expect{find('input[name="commit"]').click}.to change { Comment.count }.by(0)
  # コメントがページこのページ内に存在していないか確認する
  expect(page).to have_no_content(@comment)
end

これだけです。上記との違いは、重複されていたit文を片方削除しました。

ターミナルで実行します

% bundle exec rspec spec/system/comments_spec.rb

Comments
  コメントできるとき
    ログインしたユーザーはコメントできる(自分の投稿にコメントする場合)
    ログインしたユーザーはコメントできる(他人の投稿(@article2)にコメントする場合)
  コメントできないとき
    ログインしていないユーザーはコメントできない
    空欄のコメント送信はできない

Finished in 20.88 seconds (files took 1.77 seconds to load)
4 examples, 0 failures

うん、OKです!!

では、下記ご参考まで…
便利な翻訳サイト DeepL様
https://www.deepl.com/translator

0
0
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
0
0