LoginSignup
6
8

More than 5 years have passed since last update.

[RSpec] (feature) js: trueのテストが通ったり通らなかったり(成功・失敗を繰り返す)

Last updated at Posted at 2019-01-26

こんなfeature specを書いたのだ

spec/features/lists_operation_spec.rb
RSpec.feature "ListsOperation", type: :feature do
  feature "create and destroy lists", js: true do
    scenario "create a list" do
      visit root_path
      expect(current_path).to eq root_path
      click_on "Create List"
      expect(current_path).to eq new_list_path
      # failed with invalid parameters
      find(".form-control").set("")
      expect do
        find(".submitBtn").click
      end.not_to change(List, :count)
      expect(current_path).to eq new_list_path
      # succeeded with valid params
      find(".form-control").set("created")

      expect do
        find(".submitBtn").click
      end.to change(List, :count).by(1)

      expect(current_path).to eq root_path
      expect(page).to have_selector "h2", text: "created"
      expect(page).to have_selector "div.lists", count: lists.count + 1
      save_screenshot
    end
  end
end



すると

ListsOperation
  create and destroy lists
Capybara starting Puma...
* Version 3.12.0 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:51883
    shows lists
    create a list
    edit a list
    delete a list

Finished in 3.49 seconds (files took 0.29178 seconds to load)
4 examples, 0 failures



落ちなかったり(これが正常)

Capybara starting Puma...
* Version 3.12.0 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:51973
    shows lists
    create a list (FAILED - 1)
    edit a list
    delete a list

Failures:

  1) ListsOperation create and destroy lists create a list
     Failure/Error:
       expect do
         find(".submitBtn").click
       end.to change(List, :count).by(1)

       expected `List.count` to have changed by 1, but was changed by 0
     # ./spec/features/lists_operation_spec.rb:35:in `block (3 levels) in <main>'
     # -e:1:in `<main>'

Finished in 3.34 seconds (files took 0.30711 seconds to load)
4 examples, 1 failure



落ちたり。。。

調べたゾイ

参考:Rspecのフィーチャーテストが失敗したりしなかったり(StackOverFlow)
https://ja.stackoverflow.com/questions/463/rspec%E3%81%AE%E3%83%95%E3%82%A3%E3%83%BC%E3%83%81%E3%83%A3%E3%83%BC%E3%83%86%E3%82%B9%E3%83%88%E3%81%8C%E5%A4%B1%E6%95%97%E3%81%97%E3%81%9F%E3%82%8A%E3%81%97%E3%81%AA%E3%81%8B%E3%81%A3%E3%81%9F%E3%82%8A


js: trueを付けるとJSの処理が終わる前にRSpec側が先に進んでしまい、
本来通るはずのテストがパスしない、もしくはタイミングによってパスするときと失敗するときがある、
ということは僕も良く体験しています。

へ〜〜〜〜〜〜〜〜!!!!!!!!!!

そうなんだ。。
というわけで、該当部の下にsleep 3をつけて試してみる。

spec/features/lists_operation_spec.rb
      find(".form-control").set("created")

      expect do
        find(".submitBtn").click
      end.to change(List, :count).by(1)

      sleep 3  # ←これ

      expect(current_path).to eq root_path



すると、何回繰り返しても

ListsOperation
  create and destroy lists
Capybara starting Puma...
* Version 3.12.0 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:51883
    shows lists
    create a list
    edit a list
    delete a list

Finished in 3.49 seconds (files took 0.29178 seconds to load)
4 examples, 0 failures

Oh my goodness

素晴らしいですね。
ですが、sleep 3だとテストに時間がかかってしまうので、sleep 0.5にしました。

spec/features/lists_operation_spec.rb
      find(".form-control").set("created")

      expect do
        find(".submitBtn").click
      end.to change(List, :count).by(1)

      sleep 0.5  # ←3から0.5にしたゾイ

      expect(current_path).to eq root_path

というわけで

無事解決!!

6
8
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
6
8