Skip to content

Commit

Permalink
Merge pull request #1477 from tjallingvanderwal/document-with-satisfy
Browse files Browse the repository at this point in the history
Document receive(:msg).with(satisfy{|arg| ... })
  • Loading branch information
pirj authored and JonRowe committed Feb 4, 2024
1 parent 6b22acf commit 7ea61c6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ expect(double).to receive(:msg).with(1, duck_type(:abs, :div), "b") #2nd argumen
expect(double).to receive(:msg).with(hash_including(:a => 5)) # first arg is a hash with a: 5 as one of the key-values
expect(double).to receive(:msg).with(array_including(5)) # first arg is an array with 5 as one of the key-values
expect(double).to receive(:msg).with(hash_excluding(:a => 5)) # first arg is a hash without a: 5 as one of the key-values
expect(double).to receive(:msg).with(start_with('a')) # any matcher, custom or from rspec-expectations
expect(double).to receive(:msg).with(satisfy { |data| data.dig(:a, :b, :c) == 5 }) # assert anything you want
```

## Receive Counts
Expand Down
31 changes: 31 additions & 0 deletions features/setting_constraints/matching_arguments.feature
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@ Feature: Matching arguments
| expected: (a collection containing exactly 1 and 2) |
| got: ([1, 3]) |

@ripper
Scenario: Using satisfy for complex custom expecations
Given a file named "rspec_satisfy_spec.rb" with:
"""ruby
RSpec.describe "Using satisfy for complex custom expecations" do
let(:dbl) { double }
def a_b_c_equals_5
satisfy { |data| data[:a][:b][:c] == 5 }
end
it "passes when the expectation is true" do
expect(dbl).to receive(:foo).with(a_b_c_equals_5)
dbl.foo({ :a => { :b => { :c => 5 } } })
end
it "fails when the expectation is false" do
expect(dbl).to receive(:foo).with(a_b_c_equals_5)
dbl.foo({ :a => { :b => { :c => 3 } } })
end
end
"""
When I run `rspec rspec_satisfy_spec.rb`
Then it should fail with the following output:
| 2 examples, 1 failure |
| |
| Failure/Error: dbl.foo({ :a => { :b => { :c => 3 } } }) |
| #<Double (anonymous)> received :foo with unexpected arguments |
| expected: (satisfy expression `data[:a][:b][:c] == 5`) |
| got: ({:a=>{:b=>{:c=>3}}}) |

Scenario: Using a custom matcher
Given a file named "custom_matcher_spec.rb" with:
"""ruby
Expand Down
11 changes: 11 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
end
end

Before('@ripper') do |scenario|
unless RSpec::Support::RubyFeatures.ripper_supported?
warn "Skipping scenario due to lack of Ripper support"
if Cucumber::VERSION.to_f >= 3.0
skip_this_scenario
else
scenario.skip_invoke!
end
end
end

Before('@kw-arguments') do |scenario|
unless RSpec::Support::RubyFeatures.kw_args_supported?
warn "Skipping scenario due to lack of keyword argument support"
Expand Down

0 comments on commit 7ea61c6

Please sign in to comment.