Skip to content

Commit

Permalink
Raise error on multiple cout constraints.
Browse files Browse the repository at this point in the history
In particular, it would be reasonable to expect yield_control.at_least(1).at_most(4).times to work
  • Loading branch information
marcandre committed Mar 15, 2020
1 parent ac3dc7f commit 6e9f6c3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/rspec/matchers/built_in/yield.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions spec/rspec/matchers/built_in/yield_spec.rb
Expand Up @@ -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(/multiple/i)
end

context "with exact count" do
it 'fails if the block yields wrong number of times' do
expect {
Expand Down

0 comments on commit 6e9f6c3

Please sign in to comment.