
モバイルアプリのテストで、画面を開くたびにその画面の構成パーツ(画面要素)をチェックしているのだけど、PageObjectにcheck_all_elements()というメソッドを持たせそこでチェック。NGの場合だけ例外を投げるようにしてみた。利用はこんな感じ。
expect{HomePage.check_all_elements}.to_not raise_error(Selenium::WebDriver::Error::NoSuchElementError)
これを実行すると、以下のように怒られる。
https://gist.github.com/daipresents/a0be49b8b20fe11460864819228ccb23.js
WARNING: Using `expect { }.not_to raise_error(SpecificErrorClass)` risks false positives
のfalse positivesは誤検出という意味らしい。要約すると、
`expect { }.not_to raise_error(SpecificErrorClass)`
には誤検出のリスクがあります。なぜなら、文字通り、他のどんなエラー(たとえば、NoMethodErrorとかNameErrorやArgumentError)がRubyによって投げられても、パスしてしまうからです。なのでおそらくやりたいことと違うはず。だから、そのかわりに、`expect {}.not_to raise_error` or `expect { }.to raise_error(DifferentSpecificErrorClass)`
を検討してください。ちなみに、この警告メッセージは`RSpec::Expectations.configuration.on_potential_false_positives = :nothing`
で抑制できます。
たしかに。ということで、例外を特定しない書き方にしてみた。
expect{HomePage.check_all_elements}.to_not raise_error