Namespace
Methods
A
R
S
T
Constants
Assertion = ActiveSupport::TestCase::Assertion
 
Instance Public methods
assert_failure(message, &block)
# File actionpack/test/controller/assert_select_test.rb, line 69
def assert_failure(message, &block)
  e = assert_raise(Assertion, &block)
  assert_match(message, e.message) if Regexp === message
  assert_equal(message, e.message) if String === message
end
setup()
# File actionpack/test/controller/assert_select_test.rb, line 57
def setup
  super
  ActionMailer::Base.delivery_method = :test
  ActionMailer::Base.perform_deliveries = true
  ActionMailer::Base.deliveries = []
end
teardown()
# File actionpack/test/controller/assert_select_test.rb, line 64
def teardown
  super
  ActionMailer::Base.deliveries.clear
end
test_assert_select()

Test assert select.

# File actionpack/test/controller/assert_select_test.rb, line 79
def test_assert_select
  render_html %Q{<div id="1"></div><div id="2"></div>}
  assert_select "div", 2
  assert_failure(/Expected at least 1 element matching \"p\", found 0/) { assert_select "p" }
end
test_assert_select_email()

Test assert_select_email

# File actionpack/test/controller/assert_select_test.rb, line 320
def test_assert_select_email
  assert_raise(Assertion) { assert_select_email {} }
  AssertSelectMailer.test("<div><p>foo</p><p>bar</p></div>").deliver
  assert_select_email do
    assert_select "div:root" do
      assert_select "p:first-child", "foo"
      assert_select "p:last-child", "bar"
    end
  end
end
test_assert_select_email_multipart()
# File actionpack/test/controller/assert_select_test.rb, line 331
def test_assert_select_email_multipart
  AssertMultipartSelectMailer.test(:html => "<div><p>foo</p><p>bar</p></div>", :text => 'foo bar').deliver
  assert_select_email do
    assert_select "div:root" do
      assert_select "p:first-child", "foo"
      assert_select "p:last-child", "bar"
    end
  end
end
test_assert_select_text_match()
# File actionpack/test/controller/assert_select_test.rb, line 214
def test_assert_select_text_match
  render_html %Q{<div id="1"><span>foo</span></div><div id="2"><span>bar</span></div>}
  assert_select "div" do
    assert_nothing_raised    { assert_select "div", "foo" }
    assert_nothing_raised    { assert_select "div", "bar" }
    assert_nothing_raised    { assert_select "div", /\w*/ }
    assert_nothing_raised    { assert_select "div", :text => /\w*/, :count=>2 }
    assert_raise(Assertion)  { assert_select "div", :text=>"foo", :count=>2 }
    assert_nothing_raised    { assert_select "div", :html=>"<span>bar</span>" }
    assert_nothing_raised    { assert_select "div", :html=>"<span>bar</span>" }
    assert_nothing_raised    { assert_select "div", :html=>/\w*/ }
    assert_nothing_raised    { assert_select "div", :html=>/\w*/, :count=>2 }
    assert_raise(Assertion)  { assert_select "div", :html=>"<span>foo</span>", :count=>2 }
  end
end
test_counts()
# File actionpack/test/controller/assert_select_test.rb, line 144
def test_counts
  render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
  assert_nothing_raised               { assert_select "div", 2 }
  assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) do
    assert_select "div", 3
  end
  assert_nothing_raised               { assert_select "div", 1..2 }
  assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
    assert_select "div", 3..4
  end
  assert_nothing_raised               { assert_select "div", :count=>2 }
  assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) do
    assert_select "div", :count=>3
  end
  assert_nothing_raised               { assert_select "div", :minimum=>1 }
  assert_nothing_raised               { assert_select "div", :minimum=>2 }
  assert_failure(/Expected at least 3 elements matching \"div\", found 2/) do
    assert_select "div", :minimum=>3
  end
  assert_nothing_raised               { assert_select "div", :maximum=>2 }
  assert_nothing_raised               { assert_select "div", :maximum=>3 }
  assert_failure(/Expected at most 1 element matching \"div\", found 2/) do
    assert_select "div", :maximum=>1
  end
  assert_nothing_raised               { assert_select "div", :minimum=>1, :maximum=>2 }
  assert_failure(/Expected between 3 and 4 elements matching \"div\", found 2/) do
    assert_select "div", :minimum=>3, :maximum=>4
  end
end
test_css_select()

Test css_select.

# File actionpack/test/controller/assert_select_test.rb, line 239
def test_css_select
  render_html %Q{<div id="1"></div><div id="2"></div>}
  assert_equal 2, css_select("div").size
  assert_equal 0, css_select("p").size
end
test_elect_with_xml_namespace_attributes()
# File actionpack/test/controller/assert_select_test.rb, line 230
def test_elect_with_xml_namespace_attributes
  render_html %Q{<link xlink:href="http://nowhere.com"></link>}
  assert_nothing_raised { assert_select "link[xlink:href=http://nowhere.com]" }
end
test_equality_false_message()
# File actionpack/test/controller/assert_select_test.rb, line 101
def test_equality_false_message
  render_html %Q{<div id="1"></div><div id="2"></div>}
  assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", false }
end
test_equality_integer()
# File actionpack/test/controller/assert_select_test.rb, line 85
def test_equality_integer
  render_html %Q{<div id="1"></div><div id="2"></div>}
  assert_failure(/Expected exactly 3 elements matching \"div\", found 2/) { assert_select "div", 3 }
  assert_failure(/Expected exactly 0 elements matching \"div\", found 2/) { assert_select "div", 0 }
end
test_equality_of_html()
# File actionpack/test/controller/assert_select_test.rb, line 119
def test_equality_of_html
  render_html %Q{<p>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</p>}
  text = "\"This is not a big problem,\" he said."
  html = "<em>\"This is <strong>not</strong> a big problem,\"</em> he said."
  assert_nothing_raised    { assert_select "p", text }
  assert_raise(Assertion) { assert_select "p", html }
  assert_nothing_raised    { assert_select "p", :html=>html }
  assert_raise(Assertion) { assert_select "p", :html=>text }
  # No stripping for pre.
  render_html %Q{<pre>\n<em>"This is <strong>not</strong> a big problem,"</em> he said.\n</pre>}
  text = "\n\"This is not a big problem,\" he said.\n"
  html = "\n<em>\"This is <strong>not</strong> a big problem,\"</em> he said.\n"
  assert_nothing_raised    { assert_select "pre", text }
  assert_raise(Assertion) { assert_select "pre", html }
  assert_nothing_raised    { assert_select "pre", :html=>html }
  assert_raise(Assertion) { assert_select "pre", :html=>text }
end
test_equality_string_and_regexp()
# File actionpack/test/controller/assert_select_test.rb, line 106
def test_equality_string_and_regexp
  render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
  assert_nothing_raised    { assert_select "div", "foo" }
  assert_raise(Assertion) { assert_select "div", "bar" }
  assert_nothing_raised    { assert_select "div", :text=>"foo" }
  assert_raise(Assertion) { assert_select "div", :text=>"bar" }
  assert_nothing_raised    { assert_select "div", /(foo|bar)/ }
  assert_raise(Assertion) { assert_select "div", /foobar/ }
  assert_nothing_raised    { assert_select "div", :text=>/(foo|bar)/ }
  assert_raise(Assertion) { assert_select "div", :text=>/foobar/ }
  assert_raise(Assertion) { assert_select "p", :text=>/foobar/ }
end
test_equality_true_false()
# File actionpack/test/controller/assert_select_test.rb, line 91
def test_equality_true_false
  render_html %Q{<div id="1"></div><div id="2"></div>}
  assert_nothing_raised    { assert_select "div" }
  assert_raise(Assertion) { assert_select "p" }
  assert_nothing_raised    { assert_select "div", true }
  assert_raise(Assertion) { assert_select "p", true }
  assert_raise(Assertion) { assert_select "div", false }
  assert_nothing_raised    { assert_select "p", false }
end
test_feed_item_encoded()
# File actionpack/test/controller/assert_select_test.rb, line 263
  def test_feed_item_encoded
    render_xml <<-EOF
<rss version="2.0">
  <channel>
    <item>
      <description>
        <![CDATA[
          <p>Test 1</p>
        ]]>
      </description>
    </item>
    <item>
      <description>
        <![CDATA[
          <p>Test 2</p>
        ]]>
      </description>
    </item>
  </channel>
</rss>
EOF
    assert_select "channel item description" do
      # Test element regardless of wrapper.
      assert_select_encoded do
        assert_select "p", :count=>2, :text=>/Test/
      end
      # Test through encoded wrapper.
      assert_select_encoded do
        assert_select "encoded p", :count=>2, :text=>/Test/
      end
      # Use :root instead (recommended)
      assert_select_encoded do
        assert_select ":root p", :count=>2, :text=>/Test/
      end
      # Test individually.
      assert_select "description" do |elements|
        assert_select_encoded elements[0] do
          assert_select "p", "Test 1"
        end
        assert_select_encoded elements[1] do
          assert_select "p", "Test 2"
        end
      end
    end

    # Test that we only un-encode element itself.
    assert_select "channel item" do
      assert_select_encoded do
        assert_select "p", 0
      end
    end
  end
test_nested_assert_select()
# File actionpack/test/controller/assert_select_test.rb, line 188
def test_nested_assert_select
  render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
  assert_select "div" do |elements|
    assert_equal 2, elements.size
    assert_select elements[0], "#1"
    assert_select elements[1], "#2"
  end
  assert_select "div" do
    assert_select "div" do |elements|
      assert_equal 2, elements.size
      # Testing in a group is one thing
      assert_select "#1,#2"
      # Testing individually is another.
      assert_select "#1"
      assert_select "#2"
      assert_select "#3", false
    end
  end

  assert_failure(/Expected at least 1 element matching \"#4\", found 0\./) do
    assert_select "div" do
      assert_select "#4"
    end
  end
end
test_nested_css_select()
# File actionpack/test/controller/assert_select_test.rb, line 245
def test_nested_css_select
  render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
  assert_select "div#?", /\d+/ do |elements|
    assert_equal 1, css_select(elements[0], "div").size
    assert_equal 1, css_select(elements[1], "div").size
  end
  assert_select "div" do
    assert_equal 2, css_select("div").size
    css_select("div").each do |element|
      # Testing as a group is one thing
      assert !css_select("#1,#2").empty?
      # Testing individually is another
      assert !css_select("#1").empty?
      assert !css_select("#2").empty?
    end
  end
end
test_strip_textarea()
# File actionpack/test/controller/assert_select_test.rb, line 137
def test_strip_textarea
  render_html %Q{<textarea>\n\nfoo\n</textarea>}
  assert_select "textarea", "\nfoo\n"
  render_html %Q{<textarea>\nfoo</textarea>}
  assert_select "textarea", "foo"
end
test_substitution_values()
# File actionpack/test/controller/assert_select_test.rb, line 174
def test_substitution_values
  render_html %Q{<div id="1">foo</div><div id="2">foo</div>}
  assert_select "div#?", /\d+/ do |elements|
    assert_equal 2, elements.size
  end
  assert_select "div" do
    assert_select "div#?", /\d+/ do |elements|
      assert_equal 2, elements.size
      assert_select "#1"
      assert_select "#2"
    end
  end
end
Instance Protected methods
render_html(html)
# File actionpack/test/controller/assert_select_test.rb, line 342
def render_html(html)
  @controller.response_with = html
  get :html
end
render_xml(xml)
# File actionpack/test/controller/assert_select_test.rb, line 347
def render_xml(xml)
  @controller.response_with = xml
  get :xml
end