raise_error 匹配器

使用raise_error 匹配器指定代码块是否抛出错误。最基本的形式是如果抛出任何错误则通过

  expect { raise StandardError }.to raise_error

如果你更喜欢那个措辞,可以使用raise_exception

  expect { 3 / 0 }.to raise_exception

raise_errorraise_exception 在功能上是可互换的,因此在任何给定上下文中使用最适合你的那个。

除了上面的基本形式之外,还有许多方法可以指定错误/异常的详细信息

  expect { raise "oops" }.to raise_error
  expect { raise "oops" }.to raise_error(RuntimeError)
  expect { raise "oops" }.to raise_error("oops")
  expect { raise "oops" }.to raise_error(/op/)
  expect { raise "oops" }.to raise_error(RuntimeError, "oops")
  expect { raise "oops" }.to raise_error(RuntimeError, /op/)
  expect { raise "oops" }.to raise_error(an_instance_of(RuntimeError).and having_attributes(message: "oops"))

期望任何错误

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

RSpec.describe "calling a missing method" do
  it "raises" do
    expect { Object.new.foo }.to raise_error
  end
end

我运行rspec example_spec

那么该示例应该通过。

期望特定错误

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

RSpec.describe "calling a missing method" do
  it "raises" do
    expect { Object.new.foo }.to raise_error(NameError)
  end
end

我运行rspec example_spec

那么该示例应该通过。

使用字符串匹配消息

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

RSpec.describe "matching error message with string" do
  it "matches the error message" do
    expect { raise StandardError, 'this message exactly'}.
      to raise_error('this message exactly')
  end
end

我运行rspec example_spec.rb

那么该示例应该通过。

使用正则表达式匹配消息

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

RSpec.describe "matching error message with regex" do
  it "matches the error message" do
    expect { raise StandardError, "my message" }.
      to raise_error(/my mess/)
  end
end

我运行rspec example_spec.rb

那么该示例应该通过。

使用with_message 匹配消息

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

RSpec.describe "matching error message with regex" do
  it "matches the error message" do
    expect { raise StandardError, "my message" }.
      to raise_error.with_message(/my mess/)
  end
end

我运行rspec example_spec.rb

那么该示例应该通过。

使用字符串匹配类 + 消息

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

RSpec.describe "matching error message with string" do
  it "matches the error message" do
    expect { raise StandardError, 'this message exactly'}.
      to raise_error(StandardError, 'this message exactly')
  end
end

我运行rspec example_spec.rb

那么该示例应该通过。

使用正则表达式匹配类 + 消息

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

RSpec.describe "matching error message with regex" do
  it "matches the error message" do
    expect { raise StandardError, "my message" }.
      to raise_error(StandardError, /my mess/)
  end
end

我运行rspec example_spec.rb

那么该示例应该通过。

对传递给块的错误对象设置期望

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

RSpec.describe "#foo" do
  it "raises NameError" do
    expect { Object.new.foo }.to raise_error { |error|
      expect(error).to be_a(NameError)
    }
  end
end

我运行rspec example_spec

那么该示例应该通过。

使用链式匹配器对错误对象设置期望

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

RSpec.describe "composing matchers" do
  it "raises StandardError" do
    expect { raise StandardError, "my message" }.
      to raise_error(an_instance_of(StandardError).and having_attributes({"message" => "my message"}))
  end
end

我运行rspec example_spec

那么该示例应该通过。

根本不期望错误

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

RSpec.describe "#to_s" do
  it "does not raise" do
    expect { Object.new.to_s }.not_to raise_error
  end
end

我运行rspec example_spec

那么该示例应该通过。