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

Commit

Permalink
Require an argument for the Include matcher
Browse files Browse the repository at this point in the history
There are some edge-case ways you can try to invoke this matcher that
end up parsing as an argument-less call and a separate expression; this
will stop that from happening.
  • Loading branch information
nevinera committed Aug 18, 2024
1 parent ff04d47 commit bd5d306
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/rspec/matchers/built_in/include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Include < BaseMatcher # rubocop:disable Metrics/ClassLength

# @api private
def initialize(*expecteds)
raise(ArgumentError, 'Include matcher requires at least one argument') if expecteds.empty?
@expecteds = expecteds
end

Expand Down
6 changes: 6 additions & 0 deletions spec/rspec/matchers/built_in/include_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit bd5d306

Please sign in to comment.