Skip to content

Instantly share code, notes, and snippets.

@sunaot
Created January 13, 2015 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunaot/a840588ca1b7491e42b8 to your computer and use it in GitHub Desktop.
Save sunaot/a840588ca1b7491e42b8 to your computer and use it in GitHub Desktop.
shared_example ぇぇぇ

たとえば、subject でこのように書けて、

subject { model.errors }
it { is_expected.to be > 0 }

これを DRY にしたくて

subject { model.errors }
it { is_expected_to_be_invalid }

def is_expected_to_be_invalid
  is_expected.to be > 0
end

というだけの話ではないかと思います。 これを実現するのに shared_example を使うのはやりがちな罠ですがオーバースペックだと私は思ってます。

都度判断ですが、一つの期待結果として抽象化可能 (同値) であればメソッドつくればいいですし、example として期待結果の意味するところが違うなら実装が同じであっても抽象化しないほうがよいです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment