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

Document receive(:msg).with(satisfy{|arg| ... }) #1477

Merged
merged 8 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading