From 8340e1161095460fffc83260289444e94aaaa89d Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Sat, 16 Feb 2013 10:02:32 +0100 Subject: [PATCH] Add have_title RSpec matcher --- lib/capybara/rspec/matchers.rb | 66 ++++++++++++++++++++++++---------- spec/rspec/matchers_spec.rb | 36 +++++++++++++++++++ 2 files changed, 83 insertions(+), 19 deletions(-) diff --git a/lib/capybara/rspec/matchers.rb b/lib/capybara/rspec/matchers.rb index d68fcd23..4ae8721b 100644 --- a/lib/capybara/rspec/matchers.rb +++ b/lib/capybara/rspec/matchers.rb @@ -1,6 +1,16 @@ module Capybara module RSpecMatchers - class HaveSelector + class Matcher + def wrap(actual) + if actual.respond_to?("has_selector?") + actual + else + Capybara.string(actual.to_s) + end + end + end + + class HaveSelector < Matcher def initialize(*args) @args = args end @@ -17,20 +27,12 @@ module Capybara "have #{query.description}" end - def wrap(actual) - if actual.respond_to?("has_selector?") - actual - else - Capybara.string(actual.to_s) - end - end - def query @query ||= Capybara::Query.new(*@args) end end - class HaveText + class HaveText < Matcher attr_reader :text def initialize(text) @@ -59,20 +61,42 @@ module Capybara "have text #{format(text)}" end - def wrap(actual) - if actual.respond_to?("has_selector?") - actual - else - Capybara.string(actual.to_s) - end - end - def format(text) text = Capybara::Helpers.normalize_whitespace(text) unless text.is_a? Regexp text.inspect end end + class HaveTitle < Matcher + attr_reader :title + + def initialize(title) + @title = title + end + + def matches?(actual) + @actual = wrap(actual) + @actual.has_title?(title) + end + + def does_not_match?(actual) + @actual = wrap(actual) + @actual.has_no_title?(title) + end + + def failure_message_for_should + "expected there to be title #{title.inspect} in #{@actual.title.inspect}" + end + + def failure_message_for_should_not + "expected there not to be title #{title.inspect} in #{@actual.title.inspect}" + end + + def description + "have title #{title.inspect}" + end + end + def have_selector(*args) HaveSelector.new(*args) end @@ -81,7 +105,7 @@ module Capybara HaveSelector.new(:xpath, xpath, options) end - def have_css(css, options={}) + def have_css(css, options={}) HaveSelector.new(:css, css, options) end @@ -93,6 +117,10 @@ module Capybara HaveText.new(text) end + def have_title(title) + HaveTitle.new(title) + end + def have_link(locator, options={}) HaveSelector.new(:link, locator, options) end diff --git a/spec/rspec/matchers_spec.rb b/spec/rspec/matchers_spec.rb index ee4c996b..ae091733 100644 --- a/spec/rspec/matchers_spec.rb +++ b/spec/rspec/matchers_spec.rb @@ -412,6 +412,42 @@ describe Capybara::RSpecMatchers do end end + describe "have_title matcher" do + it "gives proper description" do + have_title('Just a title').description.should == "have title \"Just a title\"" + end + + context "on a string" do + let(:html) { 'Just a title' } + + it "passes if there is such a title" do + html.should have_title('Just a title') + end + + it "fails if there is no such title" do + expect do + html.should have_title('No such title') + end.to raise_error(/expected there to be title "No such title"/) + end + end + + context "on a page or node" do + before do + visit('/with_js') + end + + it "passes if there is such a title" do + page.should have_title('with_js') + end + + it "fails if there is no such title" do + expect do + page.should have_title('No such title') + end.to raise_error(/expected there to be title "No such title"/) + end + end + end + describe "have_button matcher" do let(:html) { '' }