diff --git a/README.md b/README.md index c1cd5909d..e569feee5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/features/setting_constraints/matching_arguments.feature b/features/setting_constraints/matching_arguments.feature index fba12f1ca..caea74248 100644 --- a/features/setting_constraints/matching_arguments.feature +++ b/features/setting_constraints/matching_arguments.feature @@ -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 } } }) | + | # 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 diff --git a/features/support/env.rb b/features/support/env.rb index 7a807b2b8..fd62c4491 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -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"