Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'spec/rails'
- def with_single_tag(selector, nth_of_match = nil)
- raise "Use 1 based indexes!" if nth_of_match == 0
- with_tag(selector) do |elements|
- if !nth_of_match.nil?
- element = elements[nth_of_match - 1]
- raise "No nth match (#{nth_of_match}) for #{selector}" if element.nil?
- with_tag(element, '*') do
- yield
- end
- else
- failures = []
- # Iterate over all elements trying to find a match
- elements.each do |element|
- begin
- with_tag(element, '*') do
- yield
- end
- # Yielded without an exception - means a match was found
- return
- rescue Spec::Expectations::ExpectationNotMetError => ex
- failures << ex
- end
- end
- # Checked all of the possible matches to the selector without success
- messages = []
- failures.each_with_index do |ex, index|
- messages << "#{index + 1}. " + ex.message.gsub(/<false> is not true./, '')
- end
- raise "No match for #{selector}. #{elements.size} possible matches failed:\n\n#{messages.join("\n")}"
- end
- end
- end
- describe 'the problem' do
- before do
- @html = %{
- <table>
- <tr>
- <td>ruby</td>
- <td>mac</td>
- </tr>
- <tr>
- <td>python</td>
- <td>windows</td>
- </tr>
- </table>
- }
- end
- it "should find the problem" do
- @html.should have_tag("tr") do
- with_tag("td", :text => "ruby")
- with_tag("td", :text => "windows")
- end
- end
- it "should fail" do
- root_node = HTML::Document.new(@html).root
- assert_select root_node, "tr" do
- assert_select "td", /ruby/
- assert_select "td", /windows/
- end
- end
- it "should fail" do
- @html.should have_tag('table') do
- with_single_tag('tr') do
- with_tag("td", :text => "ruby")
- with_tag("td", :text => "windows")
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment