Add have_content matcher

This commit is contained in:
Jonas Nicklas and Nicklas Ramhöj 2011-02-10 17:15:28 +01:00
parent 764f022973
commit d0985f7f78
4 changed files with 79 additions and 1 deletions

View File

@ -12,6 +12,14 @@ module Capybara
def inspect
%(#<Capybara::Document>)
end
##
#
# @return [String] The text of the document
#
def text
find('.').text
end
end
end
end

View File

@ -17,7 +17,7 @@ module Capybara
def failure_message_for_should
if normalized.failure_message
normalized.failure_message.call(@actual)
normalized.failure_message.call(@actual, normalized)
else
"expected #{selector_name} to return something"
end
@ -57,5 +57,9 @@ module Capybara
def have_css(*args)
HaveSelector.new(:css, *args)
end
def have_content(text)
HaveSelector.new(:content, text.to_s)
end
end
end

View File

@ -87,3 +87,11 @@ Capybara.add_selector(:id) do
xpath { |id| XPath.descendant[XPath.attr(:id) == id.to_s] }
match { |value| value.is_a?(Symbol) }
end
Capybara.add_selector(:content) do
xpath { |text| XPath::HTML.content(text) }
failure_message do |page, selector|
%(expected there to be content #{selector.locator.inspect} in #{page.text.inspect})
end
end

View File

@ -207,5 +207,63 @@ describe Capybara::RSpecMatchers do
end
end
end
describe "have_content matcher" do
context "on a string" do
context "with should" do
it "passes if has_css? returns true" do
"<h1>Text</h1>".should have_content('Text')
end
it "fails if has_css? returns false" do
expect do
"<h1>Text</h1>".should have_content('No such Text')
end.to raise_error(/expected there to be content "No such Text" in "Text"/)
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
"<h1>Text</h1>".should_not have_content('No such Text')
end
it "fails if has_no_css? returns false" do
expect do
"<h1>Text</h1>".should_not have_content('Text')
end.to raise_error(/expected content "Text" not to return anything/)
end
end
end
context "on a page or node" do
before do
visit('/with_html')
end
context "with should" do
it "passes if has_css? returns true" do
page.should have_content('This is a test')
end
it "fails if has_css? returns false" do
expect do
page.should have_content('No such Text')
end.to raise_error(/expected there to be content "No such Text" in "(.*)This is a test(.*)"/)
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
page.should_not have_content('No such Text')
end
it "fails if has_no_css? returns false" do
expect do
page.should_not have_content('This is a test')
end.to raise_error(/expected content "This is a test" not to return anything/)
end
end
end
end
end