0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

rspecで負数のパターンを検証するときのwarningをどうするか

0
Last updated at Posted at 2017-03-10

rspecでこんな風な検証パターンを入れたときのこと

it { is_expected.to eq -5_000 }

そうするとrubocopでwarningが出てしまう。

W: Ambiguous negative number operator. Parenthesize the method arguments if it's surely a negative number operator, or add a whitespace to the right of the - if it should be a subtraction.

Parenthesize = かっこ ということでカッコが必要だ。
この投稿のコメントをいただいた知ったのですが () で囲んであげれば解決!

it { is_expected.to eq(-5_000) }

eqと-5_000の間に何もスペースを入れないのがよい。eq (-5_000)だとこんな風に言われてしまう。

W: (...) interpreted as grouped expression.

あとは、rubocopの設定を調整してもいいんだけど、結果、一旦変数に入れてしまえばOK

it do
  expectation = -5_000
  is_expected.to eq expectation 
end

毎回上記のような記載をするのもコード的には長くなってしまうのでshared_examplesにしてもいいですね。大体この手のパターンは多数使うこともあるでしょうし。

shared_examples do |expectation|
  is_expected.to eq expectation 
end
0
1
2

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?