期待値フレームワークの設定
デフォルトでは、RSpecは望ましい結果を表現するためにrspec-expectationsを含めるように設定されています。また、RSpecを以下のように設定することもできます。
- rspec/expectations(明示的に)
- test/unitのアサーション
- minitestのアサーション
- 上記のライブラリの組み合わせ
ただし、rspec-expectationsを使用しない場合、すべての例に明示的な説明を提供する必要があります。rspec-expectationsが提供する生成された説明に頼ることはできません。
デフォルトの設定ではrspec-expectationsが使用されます
「example_spec.rb」という名前のファイルがあるとします。
RSpec::Matchers.define :be_a_multiple_of do |factor|
match do |actual|
actual % factor == 0
end
end
RSpec.describe 6 do
it { is_expected.to be_a_multiple_of 3 }
end
rspec example_spec.rb
を実行すると、
すべての例がパスするはずです。
rspec-expectationsを明示的に設定する
「example_spec.rb」という名前のファイルがあるとします。
RSpec.configure do |config|
config.expect_with :rspec
end
RSpec.describe 5 do
it "is greater than 4" do
expect(5).to be > 4
end
end
rspec example_spec.rb
を実行すると、
すべての例がパスするはずです。
test/unitのアサーションを設定する
rspec-expectationsがインストールされていないとします。
「example_spec.rb」という名前のファイルがあるとします。
RSpec.configure do |config|
config.expect_with :test_unit
end
RSpec.describe [1] do
it "is equal to [1]" do
assert_equal [1], [1], "expected [1] to equal [1]"
end
specify { assert_not_equal [1], [] }
it "is equal to [2] (intentional failure)" do
assert [1] == [2], "errantly expected [2] to equal [1]"
end
end
rspec example_spec.rb
を実行すると、
出力は以下のようになるはずです。
(Test::Unit::AssertionFailedError|Mini(T|t)est::Assertion):
errantly expected \[2\] to equal \[1\]
また、出力には「3 examples, 1 failure」という文字列が含まれるはずです。
minitestのアサーションを設定する
rspec-expectationsがインストールされていないとします。
「example_spec.rb」という名前のファイルがあるとします。
RSpec.configure do |config|
config.expect_with :minitest
end
RSpec.describe "Object identity" do
it "the an object is the same as itself" do
x = [1]
assert_same x, x, "expected x to be the same x"
end
specify { refute_same [1], [1] }
it "is empty (intentional failure)" do
assert_empty [1], "errantly expected [1] to be empty"
end
it "marks pending for skip method" do
skip "intentionally"
end
end
rspec -b example_spec.rb
を実行すると、
出力は以下のようになるはずです。
MiniT|test::Assertion:
errantly expected \[1\] to be empty
また、出力には「4 examples, 1 failure, 1 pending」という文字列が含まれ、かつ「Warning: you should require 'minitest/autorun' instead.」という文字列は含まれないはずです。
rspec/expectationsとtest/unitのアサーションを設定する
「example_spec.rb」という名前のファイルがあるとします。
RSpec.configure do |config|
config.expect_with :rspec, :test_unit
end
RSpec.describe [1] do
it "is equal to [1]" do
assert_equal [1], [1], "expected [1] to equal [1]"
end
it "matches array [1]" do
is_expected.to match_array([1])
end
end
rspec example_spec.rb
を実行すると、
すべての例がパスするはずです。
rspec/expectationsとminitestのアサーションを設定する
Given a file named "example_spec.rb" with:
RSpec.configure do |config|
config.expect_with :rspec, :minitest
end
RSpec.describe "Object identity" do
it "two arrays are not the same object" do
refute_same [1], [1]
end
it "an array is itself" do
array = [1]
expect(array).to be array
end
end
When I run rspec example_spec.rb
Then the examples should all pass.
テストユニットとminitestのアサーションを設定する
Given rspec-expectationsがインストールされていない
And a file named "example_spec.rb" with:
RSpec.configure do |config|
config.expect_with :test_unit, :minitest
end
RSpec.describe [1] do
it "is equal to [1]" do
assert_equal [1], [1], "expected [1] to equal [1]"
end
specify { assert_not_equal [1], [] }
it "the an object is the same as itself" do
x = [1]
assert_same x, x, "expected x to be the same x"
end
specify { refute_same [1], [1] }
end
When I run rspec example_spec.rb
Then the examples should all pass.