1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
Feature: `raise_error` matcher
Use the `raise_error` matcher to specify that a block of code raises an error. The most
basic form passes if any error is thrown:
```ruby
expect { raise StandardError }.to raise_error
```
You can use `raise_exception` instead if you prefer that wording:
```ruby
expect { 3 / 0 }.to raise_exception
```
`raise_error` and `raise_exception` are functionally interchangeable, so use the one that
makes the most sense to you in any given context.
In addition to the basic form, above, there are a number of ways to specify details of an
error/exception:
```ruby
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"))
```
Scenario: Expecting any error
Given a file named "example_spec" with:
"""
RSpec.describe "calling a missing method" do
it "raises" do
expect { Object.new.foo }.to raise_error
end
end
"""
When I run `rspec example_spec`
Then the example should pass
Scenario: Expecting a specific error
Given a file named "example_spec" with:
"""
RSpec.describe "calling a missing method" do
it "raises" do
expect { Object.new.foo }.to raise_error(NameError)
end
end
"""
When I run `rspec example_spec`
Then the example should pass
Scenario: Matching a message with a string
Given a file named "example_spec.rb" with:
"""ruby
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
"""
When I run `rspec example_spec.rb`
Then the example should pass
Scenario: Matching a message with a regexp
Given a file named "example_spec.rb" with:
"""ruby
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
"""
When I run `rspec example_spec.rb`
Then the example should pass
Scenario: Matching a message with `with_message`
Given a file named "example_spec.rb" with:
"""ruby
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
"""
When I run `rspec example_spec.rb`
Then the example should pass
Scenario: Matching a class + message with string
Given a file named "example_spec.rb" with:
"""ruby
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
"""
When I run `rspec example_spec.rb`
Then the example should pass
Scenario: Matching a class + message with regexp
Given a file named "example_spec.rb" with:
"""ruby
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
"""
When I run `rspec example_spec.rb`
Then the example should pass
Scenario: Setting expectations on error object passed to block
Given a file named "example_spec" with:
"""
RSpec.describe "#foo" do
it "raises NameError" do
expect { Object.new.foo }.to raise_error { |error|
expect(error).to be_a(NameError)
}
end
end
"""
When I run `rspec example_spec`
Then the example should pass
Scenario: Setting expectations on an error object with chained matchers
Given a file named "example_spec" with:
"""
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
"""
When I run `rspec example_spec`
Then the example should pass
Scenario: Expecting no error at all
Given a file named "example_spec" with:
"""
RSpec.describe "#to_s" do
it "does not raise" do
expect { Object.new.to_s }.not_to raise_error
end
end
"""
When I run `rspec example_spec`
Then the example should pass
|