1. Aug 3rd, 2006

    assert_select does RJS

    logo.jpg

    A new release, and a great addition.

    For starters, assert_select will now work with both HTML and RJS outputs. So if your code does:

    if xhr?
      render :update do |page|
        page.replace "message", "<p>Hi there</p>"
      end
    else
      render :text=>"<p>Hi there</p>"
    end

    You can test it with:

    assert_select "p", "Hi there"

    I have a lot of actions that do smart updates using RJS, but degrade nicely to update the entire page when JavaScript is disabled. An easy way to test both:

    test = lambda do
      assert_select "div#post-1" do
        assert_select "p:first-child", /Your post has been updated/
        assert_select "p:last-child", /Click here to see/
      end
    end
    
    post :update, :id=>1
    test.call
    xhr :post, :update, :id=>1
    test.call

    You can also test for RJS statements that update specific elements:

    assert_select_rjs "post-1" do
      assert_select "p:first-child", /Your post has been updated/
      assert_select "p:last-child", /Click here to see/
    end

    And even assert whether RJS is doing an update or an insert, and what type of insertion:

    assert_select_rjs :update, "notice" do
      assert_select "*", /Please enter your e-mail address/
    end
    assert_select_rjs :insert, :top do
      assert_select "div#?", /item-\d+/
    end

    To install the assert_select plugin:

    ./script/plugin install http://labnotes.org/svn/public/ruby/rails_plugins/assert_select
    1. Aug 3rd, 2006

      Labnotes » Blog Archive » assert_select plugin for Rails

      [...] Update 2:A new release adds support for RJS. [...]

    2. Aug 15th, 2006

      Benjamin Birnbaum

      Great plugin! One problem though – I just upgraded my app to the latest edge rails (REV=4764) and this broke the asset_packager plugin.

      To get it working again I changed the init.rb to look like this:

      require ‘test/unit/testcase’
      require ‘assert_select’
      require ‘html_selector’
      Test::Unit::TestCase.send :include, Test::Unit::AssertSelect

    3. Sep 5th, 2006

      Jamie van Dyke

      I’m just writing a little article about assert_select and I’ve come across a problem…I took your code from above and created:

      http://p.caboo.se/11944

      But it fails saying it expected “Spank” but got “Evil Monkey”, it would seem to not be hunting past the first item unless I’m mistaken? Shouldn’t it iterate over each div.monkey and find each MONKEY within it?

    Your comment, here ⇓