Guest User

Untitled

a guest
Jun 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. require 'spec/rails'
  2.  
  3. def with_single_tag(selector, nth_of_match = nil)
  4. raise "Use 1 based indexes!" if nth_of_match == 0
  5.  
  6. with_tag(selector) do |elements|
  7. if !nth_of_match.nil?
  8. element = elements[nth_of_match - 1]
  9. raise "No nth match (#{nth_of_match}) for #{selector}" if element.nil?
  10.  
  11. with_tag(element, '*') do
  12. yield
  13. end
  14. else
  15. failures = []
  16.  
  17. # Iterate over all elements trying to find a match
  18. elements.each do |element|
  19. begin
  20. with_tag(element, '*') do
  21. yield
  22. end
  23.  
  24. # Yielded without an exception - means a match was found
  25. return
  26. rescue Spec::Expectations::ExpectationNotMetError => ex
  27. failures << ex
  28. end
  29. end
  30.  
  31. # Checked all of the possible matches to the selector without success
  32. messages = []
  33. failures.each_with_index do |ex, index|
  34. messages << "#{index + 1}. " + ex.message.gsub(/<false> is not true./, '')
  35. end
  36. raise "No match for #{selector}. #{elements.size} possible matches failed:\n\n#{messages.join("\n")}"
  37. end
  38.  
  39. end
  40. end
  41.  
  42. describe 'the problem' do
  43. before do
  44. @html = %{
  45. <table>
  46. <tr>
  47. <td>ruby</td>
  48. <td>mac</td>
  49. </tr>
  50. <tr>
  51. <td>python</td>
  52. <td>windows</td>
  53. </tr>
  54. </table>
  55. }
  56. end
  57.  
  58. it "should find the problem" do
  59. @html.should have_tag("tr") do
  60. with_tag("td", :text => "ruby")
  61. with_tag("td", :text => "windows")
  62. end
  63. end
  64.  
  65. it "should fail" do
  66. root_node = HTML::Document.new(@html).root
  67. assert_select root_node, "tr" do
  68. assert_select "td", /ruby/
  69. assert_select "td", /windows/
  70. end
  71. end
  72.  
  73. it "should fail" do
  74. @html.should have_tag('table') do
  75. with_single_tag('tr') do
  76. with_tag("td", :text => "ruby")
  77. with_tag("td", :text => "windows")
  78. end
  79. end
  80. end
  81. end
Add Comment
Please, Sign In to add comment