end_with 匹配器

使用 end_with 匹配器指定字符串或数组以预期字符或元素结尾。

  expect("this string").to end_with "string"
  expect("this string").not_to end_with "stringy"
  expect([0, 1, 2]).to end_with 1, 2

字符串使用

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

RSpec.describe "this string" do
  it { is_expected.to end_with "string" }
  it { is_expected.not_to end_with "stringy" }

  # deliberate failures
  it { is_expected.not_to end_with "string" }
  it { is_expected.to end_with "stringy" }
end

我运行 rspec example_spec.rb

然后输出应包含所有这些

4 个示例,2 个失败
预期“this string”不以“string”结尾
预期“this string”以“stringy”结尾

数组使用

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

RSpec.describe [0, 1, 2, 3, 4] do
  it { is_expected.to end_with 4 }
  it { is_expected.to end_with 3, 4 }
  it { is_expected.not_to end_with 3 }
  it { is_expected.not_to end_with 0, 1, 2, 3, 4, 5 }

  # deliberate failures
  it { is_expected.not_to end_with 4 }
  it { is_expected.to end_with 3 }
end

我运行 rspec example_spec.rb

然后输出应包含所有这些

6 个示例,2 个失败
预期 [0, 1, 2, 3, 4] 不以 4 结尾
预期 [0, 1, 2, 3, 4] 以 3 结尾