From 21fc0d68382846046cdd97cd9a87b50175b7d6eb Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Mon, 19 Aug 2024 11:10:26 +0100 Subject: [PATCH] Merge pull request #1479 from nevinera/nev--1478--include-with-no-args-should-fail Require an argument for the Include matcher --- Changelog.md | 1 + lib/rspec/matchers/built_in/include.rb | 1 + spec/rspec/matchers/built_in/include_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/Changelog.md b/Changelog.md index f7481c9f1..88f0fad60 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,7 @@ Bug Fixes: (Eric Mueller, #1455) * Use `RSpec.warning` for an expectation warning rather than `Kernel.warn`. (Jon Rowe, #1472) * Prevent mismatched use of block and value matchers in compound expectations. (Phil Pirozhkov, #1476) +* Raise an error when passing no arguments to the `include` matcher. (Eric Mueller, #1479) ### 3.13.1 / 2024-06-13 [Full Changelog](http://github.com/rspec/rspec-expectations/compare/v3.13.0...v3.13.1) diff --git a/lib/rspec/matchers/built_in/include.rb b/lib/rspec/matchers/built_in/include.rb index 57573673d..f2f187de3 100644 --- a/lib/rspec/matchers/built_in/include.rb +++ b/lib/rspec/matchers/built_in/include.rb @@ -13,6 +13,7 @@ class Include < BaseMatcher # rubocop:disable Metrics/ClassLength # @api private def initialize(*expecteds) + raise(ArgumentError, 'include() is not supported, please supply an argument') if expecteds.empty? @expecteds = expecteds end diff --git a/spec/rspec/matchers/built_in/include_spec.rb b/spec/rspec/matchers/built_in/include_spec.rb index e2b32e3df..500f01b81 100644 --- a/spec/rspec/matchers/built_in/include_spec.rb +++ b/spec/rspec/matchers/built_in/include_spec.rb @@ -161,6 +161,12 @@ def hash.send; :sent; end end end + describe "expect(...).to include(with_no_args)" do + it "fails correctly" do + expect { expect([1, 2, 3]).to include }.to raise_error(ArgumentError) + end + end + describe "expect(...).to include(with_one_arg)" do it_behaves_like "an RSpec value matcher", :valid_value => [1, 2], :invalid_value => [1] do let(:matcher) { include(2) }