From 66dcafc3c7596f5f3f87707053013fcea8e0e42b Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Mon, 10 Jan 2011 23:57:54 -0600 Subject: [PATCH] add have_css and have_xpath matchers --- lib/capybara/rspec.rb | 32 ++++++++++++----------- spec/rspec_spec.rb | 60 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 19 deletions(-) diff --git a/lib/capybara/rspec.rb b/lib/capybara/rspec.rb index c2ac05c0..7a1f354c 100644 --- a/lib/capybara/rspec.rb +++ b/lib/capybara/rspec.rb @@ -22,25 +22,27 @@ module Capybara module StringMatchers extend ::RSpec::Matchers::DSL - matcher :have_selector do |*args| - match_for_should do |string| - Capybara::string(string).has_selector?(*args) - end + %w[css xpath selector].each do |type| + matcher "have_#{type}" do |*args| + match_for_should do |string| + Capybara::string(string).send("has_#{type}?", *args) + end - match_for_should_not do |string| - Capybara::string(string).has_no_selector?(*args) - end + match_for_should_not do |string| + Capybara::string(string).send("has_no_#{type}?", *args) + end - failure_message_for_should do |string| - "expected selector #{formatted(args)} to return something from:\n#{string}" - end + failure_message_for_should do |string| + "expected #{type} #{formatted(args)} to return something from:\n#{string}" + end - failure_message_for_should_not do |string| - "expected selector #{formatted(args)} not to return anything from:\n#{string}" - end + failure_message_for_should_not do |string| + "expected #{type} #{formatted(args)} not to return anything from:\n#{string}" + end - def formatted(args) - args.length == 1 ? args.first.inspect : args.inspect + def formatted(args) + args.length == 1 ? args.first.inspect : args.inspect + end end end diff --git a/spec/rspec_spec.rb b/spec/rspec_spec.rb index cf37e6a7..0219e253 100644 --- a/spec/rspec_spec.rb +++ b/spec/rspec_spec.rb @@ -50,16 +50,68 @@ describe Capybara::RSpec::StringMatchers do include Capybara::RSpec::StringMatchers before { Capybara.default_selector = :css } after { Capybara.default_selector = :xpath } + describe "have_css matcher" do + context "with should" do + it "passes if has_css? returns true" do + "

Text

".should have_css('h1') + end + + it "fails if has_css? returns false" do + expect do + "

Text

".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 + "

Text

".should_not have_css('h2') + end + + it "fails if has_no_css? returns false" do + expect do + "

Text

".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 + "

Text

".should have_xpath('//h1') + end + + it "fails if has_xpath? returns false" do + expect do + "

Text

".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 + "

Text

".should_not have_xpath('//h2') + end + + it "fails if has_no_xpath? returns false" do + expect do + "

Text

".should_not have_xpath('//h1') + end.to raise_error(/expected xpath .* not to return anything/) + end + end + end + describe "have_selector matcher" do context "with should" do - it "passes if the String has_selector?" do + it "passes if has_selector? returns true" do "

Text

".should have_selector('h1') end - it "fails if the String returns false from has_selector?" do + it "fails if has_selector? returns false" do expect do "

Text

".should have_selector('h2') - end.to raise_error(/expected selector .* to return/) + end.to raise_error(/expected selector .* to return something/) end end @@ -71,7 +123,7 @@ describe Capybara::RSpec::StringMatchers do it "fails if has_no_selector? returns false" do expect do "

Text

".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