From 6c233827ad93d8b90f40b2eb06b216211ad0e7cf Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Sun, 15 Mar 2020 16:52:08 -0400 Subject: [PATCH] Raise error on multiple cout constraints. In particular, it would be reasonable to expect yield_control.at_least(1).at_most(4).times to work --- lib/rspec/matchers/built_in/yield.rb | 5 +++++ spec/rspec/matchers/built_in/yield_spec.rb | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lib/rspec/matchers/built_in/yield.rb b/lib/rspec/matchers/built_in/yield.rb index 7ca4808ff..9ae550c69 100644 --- a/lib/rspec/matchers/built_in/yield.rb +++ b/lib/rspec/matchers/built_in/yield.rb @@ -98,7 +98,9 @@ def assert_valid_expect_block! # Not intended to be instantiated directly. class YieldControl < BaseMatcher def initialize + @already_set = false at_least(:once) + @already_set = false end # @api public @@ -182,8 +184,11 @@ def supports_block_expectations? private def set_expected_yields_count(relativity, n) + raise "Multiple count constraints are not supported" if @already_set + @expectation_type = relativity @expected_yields_count = count_constraint_to_number(n) + @already_set = true end def count_constraint_to_number(n) diff --git a/spec/rspec/matchers/built_in/yield_spec.rb b/spec/rspec/matchers/built_in/yield_spec.rb index 8150ec71f..3cffe9308 100644 --- a/spec/rspec/matchers/built_in/yield_spec.rb +++ b/spec/rspec/matchers/built_in/yield_spec.rb @@ -73,6 +73,10 @@ def each_arg(*args, &block) expect { yield_control.at_most(nil) }.to raise_error(ArgumentError) end + it 'forbids multiple calls to count constraints' do + expect { yield_control.at_least(1).at_most(4).times }.to raise_error(RuntimeError) + end + context "with exact count" do it 'fails if the block yields wrong number of times' do expect {