Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added span_text_matcher_spec.rb. [#4 state:resolved]

  • Loading branch information...
commit 404391ae37cd1562a075f0dc0b2bd168115edb4d 1 parent 3477200
Stephen Anderson authored
View
8 lib/matchers/span_text_matcher.rb
@@ -4,7 +4,7 @@ module Matchers
class SpanTextMatcher
def initialize target_id, expected_text
- @xpath = "p span##{target_id}"
+ @target_id = target_id
@expected = expected_text
end
@@ -17,9 +17,13 @@ def failure_message
"\nWrong span text contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
end
+ def negative_failure_message
+ "\nShould not have matched span: #{@target_id}, with text: '#{@expected}'\n\n"
+ end
+
def extract_html_content html
doc = Hpricot.XML(html)
- elements = doc.search(@xpath)
+ elements = doc.search("p span##{@target_id}")
elements.map{|n| n.inner_text.strip}.first
end
View
39 spec/span_text_matcher_spec.rb
@@ -0,0 +1,39 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe 'span_text_matcher' do
+ it 'should find matching span' do
+ response = mock_model(Object, :body => '<p><span id="span_id">My Text</span></p>')
+ response.should have_span_text('span_id', 'My Text')
+ end
+
+ describe 'passed wrong id' do
+ it 'should not match' do
+ response = mock_model(Object, :body => '<p><span id="span_id">My Text</span></p>')
+ response.should_not have_span_text('wrong_id', 'My Text')
+ end
+ end
+
+ describe 'with non-matching expected' do
+ it 'should not match' do
+ response = mock_model(Object, :body => '<p><span id="span_id">My Text</span></p>')
+ response.should_not have_span_text('span_id', 'Some Other Text')
+ end
+ end
+
+ describe 'with normal failure' do
+ it 'should raise ExpectationNotMetError with correct message' do
+ response = mock_model Object, :body => 'Some non-matching HTML'
+ lambda do
+ response.should have_span_text('span_id', 'My Text')
+ end.should raise_error(Spec::Expectations::ExpectationNotMetError, "\nWrong span text contents.\nexpected: \"My Text\"\n found: nil\n\n")
+ end
+ end
+
+ describe 'with negative failure' do
+ it 'should raise ExpectationNotMetError' do
+ response = mock_model Object, :body => '<p><span id="span_id">My Text</span></p>'
+ lambda{response.should_not have_span_text('span_id', 'My Text')}.should raise_error(Spec::Expectations::ExpectationNotMetError,
+ "\nShould not have matched span: span_id, with text: 'My Text'\n\n")
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.