Ticket #7802: dom_assertions_docs_updated.diff
| File dom_assertions_docs_updated.diff, 5.0 kB (added by jeremymcanally, 7 months ago) |
|---|
-
actionpack/lib/action_controller/assertions/dom_assertions.rb
old new 1 1 module ActionController 2 2 module Assertions 3 # A suite of assertions for verifying the DOM structure of the provided markup. 4 # These are used for testing the structure of HTML or XML strings, usually 5 # views or HTML snippets generated by Rails. 3 6 module DomAssertions 4 # Test two HTML strings for equivalency (e.g., identical up to reordering of attributes) 7 # Test two HTML or XML strings (<tt>expected</tt> and <tt>actual</tt>) to be equivalent (i.e., 8 # identical up to the reordering of attributes). The <tt>message</tt> parameter allows you to 9 # feed in a message that is displayed upon failure (useful if you want to make a note about what 10 # part of the view isn't being rendered correctly). 5 11 # 6 12 # ==== Examples 13 # # Asserts that a link generated by Rails to the Rails website is correct 14 # assert_dom_equal "<a href=\"http://www.rubyonrails.com\">Rails</a>", link_to("Rails", "http://www.rubyonrails.com") 7 15 # 8 # # assert that the referenced method generates the appropriate HTML string9 # assert_dom_equal '<a href="http://www.example.com">Apples</a>', link_to("Apples", "http://www.example.com")16 # # Asserts that a link to an action is generated properly; gives a custom error message if it's not 17 # assert_dom_equal "<a href=\"account/login">Login</a>", link_to("Login", :action => 'login'), "Login link broken" 10 18 # 19 # # Tests that a text area tag generated properly 20 # assert_dom_equal "<textarea name=\"body\" id="body"></textarea>", text_area_tag("body") 21 # 22 # # Asserts that a form tag is properly generated 23 # assert_dom_equal " <form action=\"/posts\" method=\"post\"><div><input type=\"submit\" name=\"submit\" value=\"Save\" /></div></form>", 24 # form_tag('/posts') { submit_tag 'Save' } 25 # 26 # # Asserts that JavaScript files are included properly, even when given different paths 27 # assert_dom_equal "<script type=\"text/javascript\" src=\"/javascripts/xhr.js\"></script>", javascript_include_tag "xhr" 28 # assert_dom_equal """<script type="text/javascript" src="/javascripts/corners.js"></script> 29 # <script type=\"text/javascript" src=\"/shared/common.js\"></script>""", javascript_include_tag("corners.js", "/shared/common") 30 # 31 # # Tests for proper generation of an auto-discovery link for an action 32 # assert_dom_equal "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"http://host.com/controller/feed\" />", 33 # auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "RSS"}) 34 # 35 # # Asserts that a button to an action is generated correctly 36 # assert_dom_equal """<form method=\"post\" action=\"/controller/visit/9\" class=\"button-to\"> 37 # <div><input value=\"Go\" type=\"submit\" /></div> 38 # </form>""", button_to("Go", :action => "visit", :id => 9) 39 # 11 40 def assert_dom_equal(expected, actual, message = "") 12 41 clean_backtrace do 13 42 expected_dom = HTML::Document.new(expected).root … … 18 47 end 19 48 end 20 49 21 # Th e negated form of +assert_dom_equivalent+.50 # This is a negated form of #assert_dom_equivalent; that is, it tests that the HTML and/or XML strings are not equal. 22 51 # 23 52 # ==== Examples 53 # # Asserts that the image path is picked up properly for an image tag 54 # assert_dom_not_equal "<img src=\"images/my_assets/my_image.gif\">", image_tag("/my_assets/my_image.gif") 24 55 # 25 # # assert that the referenced method does not generate the specified HTML string26 # assert_dom_not_equal '<a href="http://www.example.com">Apples</a>', link_to("Oranges", "http://www.example.com")56 # # Asserts that a link to an action recognizes the route properly 57 # assert_dom_not_equal "<a href=\"/home/index\">", link_to(:controller => 'home', :action => 'index') 27 58 # 59 # # Makes sure the custom path for a JavaScript link is correctly rendered 60 # assert_dom_equal "<script type=\"text/javascript\" src=\"/javascripts/shared/nav.js\"></script>", javascript_include_tag "/shared/nav" 61 # 62 # # Tests that a custom route is recognized properly 63 # assert_dom_not_equal "<a href=\"/user/home/1\">", link_to(:controller => 'user', :action => 'home', :id => 1) 64 # # => passes 65 # 66 # assert_dom_equal "<a href=\"/user1\">", link_to(:controller => 'user', :action => 'home', :id => 1) 67 # # => passes 28 68 def assert_dom_not_equal(expected, actual, message = "") 29 69 clean_backtrace do 30 70 expected_dom = HTML::Document.new(expected).root