1行の構文
One-liner syntax
RSpecはsubject上で期待を設定するために1行の構文をサポートします。
RSpecはexampleで使われたマッチャーから自動で生成されるドックストリングをexampleに与える。
ドッグストリングそしてexampleで使用されたマッチャー正確にお互いに反映される状況で重複を避けることを助けるために明確に設計されている
過度に使われた時、うまく読めない出力されたドキュメントを作り出せたりまた、あなたが記述したオブジェクトを理解できないよう貢献する
RSpec supports a one-liner syntax for setting an expectation on the subject. RSpec will give the examples a doc string that is auto- generated from the matcher used in the example. This is designed specifically to help avoid duplication in situations where the doc string and the matcher used in the example mirror each other exactly. When used excessively, it can produce documentation output that does not read well or contribute to understanding the object you are describing.
これには二つの味がする
This comes in two flavors:
is_expected
はexpect(subject)
として簡単に定義される。そしてそのより新しいexpect
ベースの構文でrspec-expectations
を使っているときに設計される
is_expected
is defined simply asexpect(subject)
and is designed for
when you are using rspec-expectations with its newer expect-based syntax.
should
はrspec-expectations
がshould
ベース構文しか持っていなかった時、設計された。
しかしながら:should
構文が無効されても(単にObject#should
が削除されたに過ぎない、しかしこれはRSpec::Core::ExampleGroup#should
です)should
は利用可能かつ動作します。
should
was designed back when rspec-expectations only had a should-based
syntax. However, it continues to be available and work even if the
:should
syntax is disabled (since that merely removesObject#should
but this isRSpec::Core::ExampleGroup#should
).
Notes:
この機能はrspec-expectations
を使うときだけ利用可能です
- This feature is only available when using rspec-expectations.
1行の構文を使って定義されたexampleはthe [--example
option]を使ってコマンドラインから直接選ばれない
- Examples defined using this one-liner syntax cannot be directly selected from the command line using the
--example
option.
1行の構文はブロックのない期待値でのみ動作するそしてブロックの期待値で使えない。
- The one-liner syntax only works with non-block expectations (e.g.
expect(obj).to eq
, etc) and it cannot be used with block expectations (e.g.expect { object }
).
Implicit subject
RSpec.describe Array do
describe "when first created" do
# Rather than: # むしろこっちがいいと書いてるのだろうか?
# it "should be empty" do
# subject.should be_empty
# end
it { should be_empty }
# or
it { is_expected.to be_empty }
end
end
Run options: include {:full_description=>/Array/}
2 examples, 0 failures, 2 passed
- 以下の場合と比べてみると1行で済んでいる
RSpec.describe "something" do
it "does something" do
end
end
Explicit subject
RSpec.describe Array do
describe "with 3 items" do
subject { [1,2,3] }
it { should_not be_empty }
# or
it { is_expected.not_to be_empty }
end
end
Run options: include {:full_description=>/Array/}
2 examples, 0 failures, 2 passed
感想
exampleのコメントを書くことができないので分かりにくさがあるので使いにくそうだ。
ドックストリングとは?