Skip to content

Commit

Permalink
[expectations] Add chained matchers example to raise_error (rspec/rsp…
Browse files Browse the repository at this point in the history
…ec-expectations#1087)

* Add chained matchers example to raise_error

Being new to rspec, it wasn't clear that I could pass composed matchers to 'raise_error'.  This commit adds an example of a chained matcher being passed to 'raise_error'. This is useful for testing custom attributes on an error while still satisfying the one 'expect' statement per test case enforced by rubocop-rspec. 

See rubocop/rubocop-rspec#379 for more details.

---
This commit was imported from rspec/rspec-expectations@dc0ecfd.
  • Loading branch information
JackWink authored and JonRowe committed Nov 28, 2018
1 parent 176d1d4 commit 1469531
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions rspec-expectations/features/built_in_matchers/raise_error.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Feature: `raise_error` matcher
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: expect any error
Expand Down Expand Up @@ -131,6 +132,19 @@ Feature: `raise_error` matcher
When I run `rspec example_spec`
Then the example should pass

Scenario: set expectations on 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: expect no error at all
Given a file named "example_spec" with:
"""
Expand Down

0 comments on commit 1469531

Please sign in to comment.