Added ambiguous error

This commit is contained in:
Jonas Nicklas 2012-07-09 13:21:44 +02:00
parent 453cec449d
commit b8af344659
3 changed files with 13 additions and 6 deletions

View File

@ -7,6 +7,7 @@ module Capybara
class DriverNotFoundError < CapybaraError; end class DriverNotFoundError < CapybaraError; end
class FrozenInTime < CapybaraError; end class FrozenInTime < CapybaraError; end
class ElementNotFound < CapybaraError; end class ElementNotFound < CapybaraError; end
class Ambiguous < ElementNotFound; end
class ExpectationNotMet < ElementNotFound; end class ExpectationNotMet < ElementNotFound; end
class FileNotFound < CapybaraError; end class FileNotFound < CapybaraError; end
class UnselectNotAllowed < CapybaraError; end class UnselectNotAllowed < CapybaraError; end

View File

@ -21,14 +21,20 @@ module Capybara
end end
def find! def find!
raise Capybara::ElementNotFound, failure_message(true) if @filtered_elements.count != 1 raise find_error if @filtered_elements.count != 1
@filtered_elements.first @filtered_elements.first
end end
def failure_message(find=false) def find_error
if find if @filtered_elements.count == 0
"Unable to find #{@query.description}" Capybara::ElementNotFound.new("Unable to find #{@query.description}")
elsif @query.options[:count] elsif @filtered_elements.count > 1
Capybara::Ambiguous.new("Ambiguous match, found #{@filtered_elements.count} elements matching #{@query.description}")
end
end
def failure_message
if @query.options[:count]
"expected #{@query.description} to be returned #{@query.options[:count]} times" "expected #{@query.description} to be returned #{@query.options[:count]} times"
else else
"expected #{@query.description} to return something" "expected #{@query.description} to return something"

View File

@ -19,7 +19,7 @@ shared_examples_for "find" do
end end
it "should raise an error if there are multiple matches" do it "should raise an error if there are multiple matches" do
expect { @session.find('//a') }.to raise_error(Capybara::ElementNotFound) expect { @session.find('//a') }.to raise_error(Capybara::Ambiguous)
end end
describe 'the returned node' do describe 'the returned node' do