From 2b030c7c3e3fc4a8105a477ed7a13dce9cef0ef4 Mon Sep 17 00:00:00 2001 From: Jo Liss Date: Wed, 1 Aug 2012 21:56:11 +0200 Subject: [PATCH] Fix error messages for has_text when argument is non-string Follow-up fix to #762. --- lib/capybara/rspec/matchers.rb | 11 ++++++++--- spec/rspec/matchers_spec.rb | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/capybara/rspec/matchers.rb b/lib/capybara/rspec/matchers.rb index 47babada..d68fcd23 100644 --- a/lib/capybara/rspec/matchers.rb +++ b/lib/capybara/rspec/matchers.rb @@ -48,15 +48,15 @@ module Capybara end def failure_message_for_should - "expected there to be text #{text.inspect} in #{@actual.text.inspect}" + "expected there to be text #{format(text)} in #{format(@actual.text)}" end def failure_message_for_should_not - "expected there not to be text #{text.inspect} in #{@actual.text.inspect}" + "expected there not to be text #{format(text)} in #{format(@actual.text)}" end def description - "have text #{text.inspect}" + "have text #{format(text)}" end def wrap(actual) @@ -66,6 +66,11 @@ module Capybara 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 def have_selector(*args) diff --git a/spec/rspec/matchers_spec.rb b/spec/rspec/matchers_spec.rb index 82dd7c59..ee4c996b 100644 --- a/spec/rspec/matchers_spec.rb +++ b/spec/rspec/matchers_spec.rb @@ -324,6 +324,12 @@ describe Capybara::RSpecMatchers do "

Text

".should have_text('No such Text') end.to raise_error(/expected there to be text "No such Text" in "Text"/) end + + it "casts has_text? argument to string" do + expect do + "

Text

".should have_text(:cast_me) + end.to raise_error(/expected there to be text "cast_me" in "Text"/) + end end context "with should_not" do