Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple yield control combinations #1169

Merged
merged 4 commits into from Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/rspec/matchers/built_in/yield.rb
Expand Up @@ -202,6 +202,8 @@ def count_constraint_to_number(n)

def failure_reason
return ' but was not a block' unless @probe.has_block?
return "#{human_readable_expectation_type}#{human_readable_count(@expected_yields_count)} but did not yield" if @probe.num_yields.zero?

"#{human_readable_expectation_type}#{human_readable_count(@expected_yields_count)}" \
" but yielded#{human_readable_count(@probe.num_yields)}"
end
Expand Down
18 changes: 18 additions & 0 deletions spec/rspec/matchers/built_in/yield_spec.rb
Expand Up @@ -61,6 +61,24 @@ def each_arg(*args, &block)
}.to fail_with(/expected given block to yield control but/)
end

it 'fails if the block does not yield the correct number of times' do
expect {
expect { |b| 0.times.each(&b) }.to yield_control.at_least(:once)
}.to fail_with(/expected given block to yield control at least once but did not yield/)

expect {
expect { |b| 2.times.each(&b) }.to yield_control.at_most(:once)
}.to fail_with(/expected given block to yield control at most once but yielded twice/)

expect {
expect { |b| 1.times.each(&b) }.to yield_control.at_least(:twice)
}.to fail_with(/expected given block to yield control at least twice but yielded once/)

expect {
expect { |b| 0.times.each(&b) }.to yield_control
}.to fail_with(/expected given block to yield control but did not yield/)
end

it 'does not return a meaningful value from the block' do
val = nil
expect { |b| val = _yield_with_args(&b) }.to yield_control
Expand Down