配置期望框架

默认情况下,RSpec 配置为包含 rspec-expectations 以表达期望的结果。您也可以将 RSpec 配置为使用

请注意,当您不使用 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 个示例,1 个失败”。

配置 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 个示例,1 个失败,1 个待定”

并且输出不应包含“警告:您应该要求“minitest/autorun”而不是”。

配置 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 断言

给定一个名为“example_spec.rb”的文件,其中包含

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

我运行 rspec example_spec.rb

然后示例应该全部通过。

配置 test/unit 和 minitest 断言

给定 rspec-expectations 未安装

并且一个名为“example_spec.rb”的文件,其中包含

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

我运行 rspec example_spec.rb

然后示例应该全部通过。