1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Further improve error messages

This commit is contained in:
Jonas Nicklas 2012-07-09 14:47:11 +02:00
parent bd69a66aff
commit 28aa12ca50
2 changed files with 44 additions and 17 deletions

View file

@ -3,55 +3,82 @@ module Capybara
include Enumerable
def initialize(elements, query)
@unfiltered_elements = elements
@filtered_elements = elements.select { |node| query.matches_filters?(node) }
@elements = elements
@result = elements.select { |node| query.matches_filters?(node) }
@rest = @elements - @result
@query = query
end
def each(&block)
@filtered_elements.each(&block)
@result.each(&block)
end
def first
@filtered_elements.first
@result.first
end
def matches_count?
@query.matches_count?(@filtered_elements.size)
@query.matches_count?(@result.size)
end
def find!
raise find_error if @filtered_elements.count != 1
@filtered_elements.first
raise find_error if @result.count != 1
@result.first
end
def size; @filtered_elements.size; end
def size; @result.size; end
alias_method :length, :size
alias_method :count, :size
def find_error
if @filtered_elements.count == 0
if @result.count == 0
Capybara::ElementNotFound.new("Unable to find #{@query.description}")
elsif @filtered_elements.count > 1
elsif @result.count > 1
Capybara::Ambiguous.new("Ambiguous match, found #{size} elements matching #{@query.description}")
end
end
def failure_message
if @query.options[:count]
"expected #{@query.description} to be returned #{@query.options[:count]} times, was found #{size} times"
message = if @query.options[:count]
"expected #{@query.description} to be found #{@query.options[:count]} #{declension("time", "times", @query.options[:count])}"
elsif @query.options[:between]
"expected #{@query.description} to be found between #{@query.options[:between].first} and #{@query.options[:between].last} times"
elsif @query.options[:maximum]
"expected #{@query.description} to be found at most #{@query.options[:maximum]} #{declension("time", "times", @query.options[:maximum])}"
elsif @query.options[:minimum]
"expected #{@query.description} to be found at least #{@query.options[:minimum]} #{declension("time", "times", @query.options[:minimum])}"
else
"expected to find #{@query.description} but there were no matches"
"expected to find #{@query.description}"
end
if count > 0
message << ", found #{count} #{declension("match", "matches")}: " << @result.map(&:text).map(&:inspect).join(", ")
else
message << " but there were no matches"
end
unless @rest.empty?
elements = @rest.map(&:text).map(&:inspect).join(", ")
message << ". Also found " << elements << ", which matched the selector but not all filters."
end
message
end
def negative_failure_message
"expected not to find #{@query.description}, but there were #{size} matches"
"expected not to find #{@query.description}, but there #{declension("was", "were")} #{count} #{declension("match", "matches")}"
end
def empty?
@filtered_elements.empty?
@result.empty?
end
def [](key); @result[key]; end
private
def declension(singular, plural, count=count)
if count == 1
singular
else
plural
end
end
def [](key); @filtered_elements[key]; end
end
end

View file

@ -30,7 +30,7 @@ describe Capybara::RSpecMatchers do
it "fails if matched node count does not equal expected count" do
expect do
"<h1>Text</h1>".should have_css('h1', :count => 2)
end.to raise_error(/expected css "h1" to be returned 2 times/)
end.to raise_error(/expected css "h1" to be found 2 times/)
end
end