Skip to content

Instantly share code, notes, and snippets.

@haikuwebdev
Created January 26, 2009 21:31
Show Gist options
  • Save haikuwebdev/52976 to your computer and use it in GitHub Desktop.
Save haikuwebdev/52976 to your computer and use it in GitHub Desktop.

Revisions

  1. @tjstankus tjstankus revised this gist Jan 27, 2009. 2 changed files with 8 additions and 9 deletions.
    4 changes: 2 additions & 2 deletions my_helper_test.rb
    Original file line number Diff line number Diff line change
    @@ -2,12 +2,12 @@

    class MyHelperTest < ActionView::TestCase
    test "title" do
    assert_select '<h1>My Awesome App</h1>'.to_node, 'h1'
    assert_select_in '<h1>My Awesome App</h1>', 'h1'
    end

    test "nav" do
    html = '<div id="nav"><a href="/">Home</a></div>'
    assert_select html.to_node, 'div' do
    assert_select_in html, 'div' do
    assert_select 'a', /home/i
    end
    end
    13 changes: 6 additions & 7 deletions test_helper.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,10 @@
    # ... ENV, requires, etc.
    # Add this to the bottom of your test_helper.rb

    class String
    def to_node
    HTML::Document.new(self).root
    module HelpersSelectorAsssertions
    def assert_select_in(html, *args, &block)
    node = HTML::Document.new(html).root
    assert_select(*args.unshift(node), &block)
    end
    end

    class ActiveSupport::TestCase
    # ...
    end
    ActionView::TestCase.send(:include, HelpersSelectorAsssertions)
  2. @tjstankus tjstankus revised this gist Jan 26, 2009. 3 changed files with 12 additions and 7 deletions.
    6 changes: 0 additions & 6 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +0,0 @@
    # Put this in test_helper.rb
    class String
    def to_node
    HTML::Document.new(self).root
    end
    end
    2 changes: 1 addition & 1 deletion my_helper_test.rb
    Original file line number Diff line number Diff line change
    @@ -13,5 +13,5 @@ class MyHelperTest < ActionView::TestCase
    end
    end

    # This is probably a more flexible and elegant solution
    # A more flexible and elegant solution can be found here:
    # http://github.com/vigetlabs/helper_me_test/tree/master
    11 changes: 11 additions & 0 deletions test_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # ... ENV, requires, etc.

    class String
    def to_node
    HTML::Document.new(self).root
    end
    end

    class ActiveSupport::TestCase
    # ...
    end
  3. @tjstankus tjstankus revised this gist Jan 26, 2009. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions my_helper_test.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    require 'test_helper'

    class MyHelperTest < ActionView::TestCase
    test "title" do
    assert_select '<h1>My Awesome App</h1>'.to_node, 'h1'
    end

    test "nav" do
    html = '<div id="nav"><a href="/">Home</a></div>'
    assert_select html.to_node, 'div' do
    assert_select 'a', /home/i
    end
    end
    end

    # This is probably a more flexible and elegant solution
    # http://github.com/vigetlabs/helper_me_test/tree/master
  4. @tjstankus tjstankus created this gist Jan 26, 2009.
    6 changes: 6 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    # Put this in test_helper.rb
    class String
    def to_node
    HTML::Document.new(self).root
    end
    end