Make sure matchers support options
This commit is contained in:
parent
6d409dcf53
commit
fb16abb308
|
@ -19,12 +19,18 @@ module Capybara
|
||||||
if normalized[:selector].failure_message
|
if normalized[:selector].failure_message
|
||||||
normalized[:selector].failure_message.call(@actual)
|
normalized[:selector].failure_message.call(@actual)
|
||||||
else
|
else
|
||||||
"expected #{normalized[:selector].name} #{normalized[:locator].inspect} to return something"
|
"expected #{selector_name} to return something"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def failure_message_for_should_not
|
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
|
end
|
||||||
|
|
||||||
def wrap(actual)
|
def wrap(actual)
|
||||||
|
|
|
@ -15,15 +15,16 @@ module Capybara
|
||||||
all.delete(name.to_sym)
|
all.delete(name.to_sym)
|
||||||
end
|
end
|
||||||
|
|
||||||
def normalize(name_or_locator, locator=nil)
|
def normalize(*args)
|
||||||
result = {}
|
result = {}
|
||||||
|
result[:options] = if args.last.is_a?(Hash) then args.pop else {} end
|
||||||
|
|
||||||
if locator
|
if args[1]
|
||||||
result[:selector] = all[name_or_locator]
|
result[:selector] = all[args[0]]
|
||||||
result[:locator] = locator
|
result[:locator] = args[1]
|
||||||
else
|
else
|
||||||
result[:selector] = all.values.find { |s| s.match?(name_or_locator) }
|
result[:selector] = all.values.find { |s| s.match?(args[0]) }
|
||||||
result[:locator] = name_or_locator
|
result[:locator] = args[0]
|
||||||
end
|
end
|
||||||
result[:selector] ||= all[Capybara.default_selector]
|
result[:selector] ||= all[Capybara.default_selector]
|
||||||
|
|
||||||
|
|
|
@ -168,7 +168,7 @@ describe Capybara::RSpecMatchers do
|
||||||
|
|
||||||
context "with should" do
|
context "with should" do
|
||||||
it "passes if has_css? returns true" do
|
it "passes if has_css? returns true" do
|
||||||
page.should have_selector('//h1')
|
page.should have_selector('//h1', :text => 'test')
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if has_css? returns false" do
|
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.to raise_error(%r(expected xpath "//h1\[@id='doesnotexist'\]" to return something))
|
||||||
end
|
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
|
it "fails with the selector's failure_message if set" do
|
||||||
Capybara.add_selector(:monkey) do
|
Capybara.add_selector(:monkey) do
|
||||||
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
|
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
|
||||||
|
@ -195,8 +201,8 @@ describe Capybara::RSpecMatchers do
|
||||||
|
|
||||||
it "fails if has_no_css? returns false" do
|
it "fails if has_no_css? returns false" do
|
||||||
expect do
|
expect do
|
||||||
page.should_not have_selector(:css, 'h1')
|
page.should_not have_selector(:css, 'h1', :text => 'test')
|
||||||
end.to raise_error(%r(expected css "h1" not to return anything))
|
end.to raise_error(%r(expected css "h1" with text "test" not to return anything))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue