Rspec-expectations

提供:Dev Guides
移動先:案内検索

RSpec-期待

RSpecを学ぶとき、期待について多くを読むかもしれませんが、最初は少し混乱するかもしれません。 「期待」という用語が表示される場合、留意すべき主な2つの詳細があります-

  • Expectationは、* expect()メソッドを使用する itブロック*内のステートメントです。 それでおしまい。 それ以上に複雑ではありません。 次のようなコードがある場合:* expect(1 + 1).to eq(2)、あなたの例には期待があります。 式 *1 + 12 に評価されることを期待しています。 ただし、RSpecはBDDテストフレームワークであるため、文言は重要です。 このステートメントをExpectationと呼ぶことで、RSpecコードがテスト対象のコードの「動作」を記述していることが明確になります。 アイデアは、ドキュメントのように読み取る方法で、コードの動作方法を表現しているということです。 期待構文は比較的新しいものです。 expect()メソッドが導入される前(2012年)、RSpecは should()メソッドに基づく別の構文を使用していました。 上記のExpectationは、古い構文の(1 + 1).should eq(2)*のように記述されています。

古いコードベースまたは古いバージョンのRSpecで作業する場合、期待の古いRSpec構文が発生する場合があります。 RSpecの新しいバージョンで古い構文を使用すると、警告が表示されます。

たとえば、このコードで-

RSpec.describe "An RSpec file that uses the old syntax" do
   it 'you should see a warning when you run this Example' do
      (1 + 1).should eq(2)
   end
end

実行すると、次のような出力が得られます-

. Deprecation Warnings:

Using `should` from rspec-expectations' old `:should`
   syntax without explicitly enabling the syntax is deprecated.
   Use the new `:expect` syntax or explicitly enable

`:should` with `config.expect_with( :rspec) { |c| c.syntax = :should }`
   instead. Called from C:/rspec_tutorial/spec/old_expectation.rb:3 :in
   `block (2 levels) in <top (required)>'.

If you need more of the backtrace for any of these deprecations to
   identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the deprecation
   warnings into errors, giving you the full backtrace.

1 deprecation warning total
Finished in 0.001 seconds (files took 0.11201 seconds to load)
1 example, 0 failures

古い構文を使用する必要がない限り、should()ではなくexpect()を使用することを強くお勧めします。