Skip to content

Commit

Permalink
Added span_text_matcher_spec.rb. [#4 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Anderson committed Sep 28, 2008
1 parent 3477200 commit 404391a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/matchers/span_text_matcher.rb
Expand Up @@ -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

Expand All @@ -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

Expand Down
39 changes: 39 additions & 0 deletions 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

0 comments on commit 404391a

Please sign in to comment.