add have_css and have_xpath matchers

This commit is contained in:
David Chelimsky 2011-01-10 23:57:54 -06:00 committed by Jonas Nicklas
parent 1e7c79ffc0
commit 66dcafc3c7
2 changed files with 73 additions and 19 deletions

View File

@ -22,25 +22,27 @@ module Capybara
module StringMatchers module StringMatchers
extend ::RSpec::Matchers::DSL extend ::RSpec::Matchers::DSL
matcher :have_selector do |*args| %w[css xpath selector].each do |type|
match_for_should do |string| matcher "have_#{type}" do |*args|
Capybara::string(string).has_selector?(*args) match_for_should do |string|
end Capybara::string(string).send("has_#{type}?", *args)
end
match_for_should_not do |string| match_for_should_not do |string|
Capybara::string(string).has_no_selector?(*args) Capybara::string(string).send("has_no_#{type}?", *args)
end end
failure_message_for_should do |string| failure_message_for_should do |string|
"expected selector #{formatted(args)} to return something from:\n#{string}" "expected #{type} #{formatted(args)} to return something from:\n#{string}"
end end
failure_message_for_should_not do |string| failure_message_for_should_not do |string|
"expected selector #{formatted(args)} not to return anything from:\n#{string}" "expected #{type} #{formatted(args)} not to return anything from:\n#{string}"
end end
def formatted(args) def formatted(args)
args.length == 1 ? args.first.inspect : args.inspect args.length == 1 ? args.first.inspect : args.inspect
end
end end
end end

View File

@ -50,16 +50,68 @@ describe Capybara::RSpec::StringMatchers do
include Capybara::RSpec::StringMatchers include Capybara::RSpec::StringMatchers
before { Capybara.default_selector = :css } before { Capybara.default_selector = :css }
after { Capybara.default_selector = :xpath } after { Capybara.default_selector = :xpath }
describe "have_css matcher" do
context "with should" do
it "passes if has_css? returns true" do
"<h1>Text</h1>".should have_css('h1')
end
it "fails if has_css? returns false" do
expect do
"<h1>Text</h1>".should have_css('h2')
end.to raise_error(/expected css .* to return something/)
end
end
context "with should_not" do
it "passes if has_no_css? returns true" do
"<h1>Text</h1>".should_not have_css('h2')
end
it "fails if has_no_css? returns false" do
expect do
"<h1>Text</h1>".should_not have_css('h1')
end.to raise_error(/expected css .* not to return anything/)
end
end
end
describe "have_xpath matcher" do
context "with should" do
it "passes if has_xpath? returns true" do
"<h1>Text</h1>".should have_xpath('//h1')
end
it "fails if has_xpath? returns false" do
expect do
"<h1>Text</h1>".should have_xpath('//h2')
end.to raise_error(/expected xpath .* to return something/)
end
end
context "with should_not" do
it "passes if has_no_xpath? returns true" do
"<h1>Text</h1>".should_not have_xpath('//h2')
end
it "fails if has_no_xpath? returns false" do
expect do
"<h1>Text</h1>".should_not have_xpath('//h1')
end.to raise_error(/expected xpath .* not to return anything/)
end
end
end
describe "have_selector matcher" do describe "have_selector matcher" do
context "with should" do context "with should" do
it "passes if the String has_selector?" do it "passes if has_selector? returns true" do
"<h1>Text</h1>".should have_selector('h1') "<h1>Text</h1>".should have_selector('h1')
end end
it "fails if the String returns false from has_selector?" do it "fails if has_selector? returns false" do
expect do expect do
"<h1>Text</h1>".should have_selector('h2') "<h1>Text</h1>".should have_selector('h2')
end.to raise_error(/expected selector .* to return/) end.to raise_error(/expected selector .* to return something/)
end end
end end
@ -71,7 +123,7 @@ describe Capybara::RSpec::StringMatchers do
it "fails if has_no_selector? returns false" do it "fails if has_no_selector? returns false" do
expect do expect do
"<h1>Text</h1>".should_not have_selector('h1') "<h1>Text</h1>".should_not have_selector('h1')
end.to raise_error(/expected selector .* not to return/) end.to raise_error(/expected selector .* not to return anything/)
end end
end end
end end