Merge pull request #2263 from teamcapybara/issue_2261

"Fix" Issue #2261 - passing objects providing `to_hash` as text match in `have_text`
This commit is contained in:
Thomas Walpole 2019-10-20 20:12:10 -07:00 committed by GitHub
commit 53bcc175dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View File

@ -673,7 +673,7 @@ module Capybara
# @return [true]
#
def assert_text(*args)
_verify_text(args) do |count, query|
_verify_text(*args) do |count, query|
unless query.matches_count?(count) && (count.positive? || query.expects_none?)
raise Capybara::ExpectationNotMet, query.failure_message
end
@ -689,7 +689,7 @@ module Capybara
# @return [true]
#
def assert_no_text(*args)
_verify_text(args) do |count, query|
_verify_text(*args) do |count, query|
if query.matches_count?(count) && (count.positive? || query.expects_none?)
raise Capybara::ExpectationNotMet, query.negative_failure_message
end
@ -849,9 +849,9 @@ module Capybara
true
end
def _verify_text(query_args)
query_args = _set_query_session_options(*query_args)
query = Capybara::Queries::TextQuery.new(*query_args)
def _verify_text(type = nil, expected_text, **query_options) # rubocop:disable Style/OptionalArguments
query_options[:session_options] = session_options
query = Capybara::Queries::TextQuery.new(type, expected_text, **query_options)
synchronize(query.wait) do
yield query.resolve_for(self), query
end

View File

@ -12,7 +12,7 @@ Capybara.add_selector(:label, locator_type: [String, Symbol]) do
if options.key?(:for)
for_option = options[:for]
for_option = for_option[:id] if for_option.is_a?(Capybara::Node::Element)
if for_option && (for_option != "")
if for_option && (for_option != '')
with_attr = builder(XPath.self).add_attribute_conditions(for: for_option)
wrapped = !XPath.attr(:for) &
builder(XPath.self.descendant(*labelable_elements)).add_attribute_conditions(id: for_option)

View File

@ -111,6 +111,37 @@ Capybara::SpecHelper.spec '#has_text?' do
expect(@session).not_to have_text(/xxxxyzzz/)
end
context 'with object implementing to_s and to_hash' do
it 'should work if the object is passed alone' do
with_to_hash = Class.new do
def to_s; '42' end
def to_hash; { value: 'Other hash' } end
end.new
@session.visit('/with_html')
expect(@session).to have_text(with_to_hash)
end
it 'should work if passed with empty options' do
with_to_hash = Class.new do
def to_s; '42' end
def to_hash; { value: 'Other hash' } end
end.new
@session.visit('/with_html')
expect(@session).to have_text(:visible, with_to_hash, {})
end
it 'should fail if passed without empty options' do
with_to_hash = Class.new do
def to_s; '42' end
def to_hash; { blah: 'Other hash' } end
end.new
@session.visit('/with_html')
expect do
expect(@session).to have_text(:visible, with_to_hash)
end.to raise_error(ArgumentError)
end
end
context 'with exact: true option' do
it 'should be true if text matches exactly' do
@session.visit('/with_html')