Capybara::Result#each returns an Enumerator when called without a block

This commit is contained in:
Thomas Walpole 2016-10-18 08:54:57 -07:00
parent 7c796bb944
commit 4bd9806804
3 changed files with 30 additions and 1 deletions

View File

@ -1,8 +1,9 @@
#Edge
Release data: unreleased
Release date: unreleased
### Fixed
* Ignore specific error when qutting selenium driver instance - Issue #1773 [Dylan Reichstadt\, Thomas Walpole]
* Capybara::Result#each now returns an `Enumerator` when called without a block - Issue #1777 [Thomas Walpole]
#2.10.1
Release date: 2016-10-08

View File

@ -40,6 +40,8 @@ module Capybara
alias :index :find_index
def each(&block)
return enum_for(:each) unless block_given?
@result_cache.each(&block)
loop do
next_result = @results_enum.next

View File

@ -92,4 +92,30 @@ RSpec.describe Capybara::Result do
result.to_a
expect(result.instance_variable_get('@result_cache').size).to eq 4
end
context '#each' do
it 'lazily evaluates' do
skip 'JRuby has an issue with lazy enumerator next evaluation' if RUBY_PLATFORM == 'java'
results=[]
result.each do |el|
results << el
expect(result.instance_variable_get('@result_cache').size).to eq results.size
end
expect(results.size).to eq 4
end
context 'without a block' do
it 'returns an iterator' do
expect(result.each).to be_a(Enumerator)
end
it 'lazily evaluates' do
skip 'JRuby has an issue with lazy enumerator next evaluation' if RUBY_PLATFORM == 'java'
result.each.with_index do |el, idx|
expect(result.instance_variable_get('@result_cache').size).to eq(idx+1) # 0 indexing
end
end
end
end
end