Motomichi Works Blog

モトミチワークスブログです。その日学習したことについて書いている日記みたいなものです。

have_contentの引数に正規表現を渡す | RSpec+Capybaraその0006

サンプルコード

以下のように書けた

expect(tr_element).to have_content /^[0-9]+$/

tr_elementとなっている箇所は何か適当にfindした要素を入れてください。

以下のように括弧で括る方がrubocopの Lint/AmbiguousRegexpLiteral ルールに引っかからないかもしれない。

expect(tr_element).to have_content(/^[0-9]+$/)

Lint/AmbiguousRegexpLiteralに引っかかったときのメッセージ

Lint/AmbiguousRegexpLiteral: Ambiguous regexp literal. Parenthesize the method arguments if it's surely a regexp literal, or add a whitespace to the right of the / if it should be a division.