Make sure matchers support options

This commit is contained in:
Jonas Nicklas 2011-02-06 20:07:12 +01:00
parent 6d409dcf53
commit fb16abb308
3 changed files with 24 additions and 11 deletions

View File

@ -19,12 +19,18 @@ module Capybara
if normalized[:selector].failure_message
normalized[:selector].failure_message.call(@actual)
else
"expected #{normalized[:selector].name} #{normalized[:locator].inspect} to return something"
"expected #{selector_name} to return something"
end
end
def failure_message_for_should_not
"expected #{normalized[:selector].name} #{normalized[:locator].inspect} not to return anything"
"expected #{selector_name} not to return anything"
end
def selector_name
name = "#{normalized[:selector].name} #{normalized[:locator].inspect}"
name << " with text #{normalized[:options][:text].inspect}" if normalized[:options][:text]
name
end
def wrap(actual)

View File

@ -15,15 +15,16 @@ module Capybara
all.delete(name.to_sym)
end
def normalize(name_or_locator, locator=nil)
def normalize(*args)
result = {}
result[:options] = if args.last.is_a?(Hash) then args.pop else {} end
if locator
result[:selector] = all[name_or_locator]
result[:locator] = locator
if args[1]
result[:selector] = all[args[0]]
result[:locator] = args[1]
else
result[:selector] = all.values.find { |s| s.match?(name_or_locator) }
result[:locator] = name_or_locator
result[:selector] = all.values.find { |s| s.match?(args[0]) }
result[:locator] = args[0]
end
result[:selector] ||= all[Capybara.default_selector]

View File

@ -168,7 +168,7 @@ describe Capybara::RSpecMatchers do
context "with should" do
it "passes if has_css? returns true" do
page.should have_selector('//h1')
page.should have_selector('//h1', :text => 'test')
end
it "fails if has_css? returns false" do
@ -177,6 +177,12 @@ describe Capybara::RSpecMatchers do
end.to raise_error(%r(expected xpath "//h1\[@id='doesnotexist'\]" to return something))
end
it "includes text in error message" do
expect do
page.should have_selector("//h1", :text => 'wrong text')
end.to raise_error(%r(expected xpath "//h1" with text "wrong text" to return something))
end
it "fails with the selector's failure_message if set" do
Capybara.add_selector(:monkey) do
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
@ -195,8 +201,8 @@ describe Capybara::RSpecMatchers do
it "fails if has_no_css? returns false" do
expect do
page.should_not have_selector(:css, 'h1')
end.to raise_error(%r(expected css "h1" not to return anything))
page.should_not have_selector(:css, 'h1', :text => 'test')
end.to raise_error(%r(expected css "h1" with text "test" not to return anything))
end
end
end