0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

One-liner syntax

Posted at

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_expectedexpect(subject)として簡単に定義される。そしてそのより新しいexpectベースの構文でrspec-expectationsを使っているときに設計される

  • is_expected is defined simply as expect(subject) and is designed for
    when you are using rspec-expectations with its newer expect-based syntax.

shouldrspec-expectationsshouldベース構文しか持っていなかった時、設計された。
しかしながら: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 removes Object#should
    but this is RSpec::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のコメントを書くことができないので分かりにくさがあるので使いにくそうだ。
ドックストリングとは?

0
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?