Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 7ea61c6

Browse files
pirjJonRowe
authored andcommitted
Merge pull request #1477 from tjallingvanderwal/document-with-satisfy
Document receive(:msg).with(satisfy{|arg| ... })
1 parent 6b22acf commit 7ea61c6

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ expect(double).to receive(:msg).with(1, duck_type(:abs, :div), "b") #2nd argumen
278278
expect(double).to receive(:msg).with(hash_including(:a => 5)) # first arg is a hash with a: 5 as one of the key-values
279279
expect(double).to receive(:msg).with(array_including(5)) # first arg is an array with 5 as one of the key-values
280280
expect(double).to receive(:msg).with(hash_excluding(:a => 5)) # first arg is a hash without a: 5 as one of the key-values
281+
expect(double).to receive(:msg).with(start_with('a')) # any matcher, custom or from rspec-expectations
282+
expect(double).to receive(:msg).with(satisfy { |data| data.dig(:a, :b, :c) == 5 }) # assert anything you want
281283
```
282284

283285
## Receive Counts

features/setting_constraints/matching_arguments.feature

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,37 @@ Feature: Matching arguments
133133
| expected: (a collection containing exactly 1 and 2) |
134134
| got: ([1, 3]) |
135135

136+
@ripper
137+
Scenario: Using satisfy for complex custom expecations
138+
Given a file named "rspec_satisfy_spec.rb" with:
139+
"""ruby
140+
RSpec.describe "Using satisfy for complex custom expecations" do
141+
let(:dbl) { double }
142+
143+
def a_b_c_equals_5
144+
satisfy { |data| data[:a][:b][:c] == 5 }
145+
end
146+
147+
it "passes when the expectation is true" do
148+
expect(dbl).to receive(:foo).with(a_b_c_equals_5)
149+
dbl.foo({ :a => { :b => { :c => 5 } } })
150+
end
151+
152+
it "fails when the expectation is false" do
153+
expect(dbl).to receive(:foo).with(a_b_c_equals_5)
154+
dbl.foo({ :a => { :b => { :c => 3 } } })
155+
end
156+
end
157+
"""
158+
When I run `rspec rspec_satisfy_spec.rb`
159+
Then it should fail with the following output:
160+
| 2 examples, 1 failure |
161+
| |
162+
| Failure/Error: dbl.foo({ :a => { :b => { :c => 3 } } }) |
163+
| #<Double (anonymous)> received :foo with unexpected arguments |
164+
| expected: (satisfy expression `data[:a][:b][:c] == 5`) |
165+
| got: ({:a=>{:b=>{:c=>3}}}) |
166+
136167
Scenario: Using a custom matcher
137168
Given a file named "custom_matcher_spec.rb" with:
138169
"""ruby

features/support/env.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@
2121
end
2222
end
2323

24+
Before('@ripper') do |scenario|
25+
unless RSpec::Support::RubyFeatures.ripper_supported?
26+
warn "Skipping scenario due to lack of Ripper support"
27+
if Cucumber::VERSION.to_f >= 3.0
28+
skip_this_scenario
29+
else
30+
scenario.skip_invoke!
31+
end
32+
end
33+
end
34+
2435
Before('@kw-arguments') do |scenario|
2536
unless RSpec::Support::RubyFeatures.kw_args_supported?
2637
warn "Skipping scenario due to lack of keyword argument support"

0 commit comments

Comments
 (0)