1. Jul 30th, 2006

    assert_select: Rails core and handling lists and tables

    logo.jpg

    assert_select is up for inclusion in Rails core. It simplified my life, and I hope we can do the same for other Rails developers. So head over to the Ruby on Rails blog and voice your opinion.

    And incidentally, I just did a major new release of assert_select, adding some features I needed for my recent test cases. The most important one, it’s now easier to test lists, tables, forms and anything that has structure and repetition using nested assertions.

    For example:

    assert_select “form[action=http://test.host/login]” do
        assert_select “input[name=username]”
        assert_select “input[name=password]”
    end
    
    assert_select “ol>li” do
        # List item has an ID we can relate to.
        assert_select “li#?”, /item-\d+/
        assert_select “p”
        # And a link to that resource.
        assert_select “a[href=?]”, /item\/\d+/
    end

    Testing flash[:notice]:

    assert_select "div#notice", flash[:notice] || false

    Testing for text will now match (string or regular expression) every selected element against that text. And as you can see from the above examples, it’s now easier to work with substitution values.

    Install and use with:

    ./script/plugins install http://labnotes.org/svn/public/ruby/rails_plugins/assert_select
    1. Jul 30th, 2006

      Labnotes » Blog Archive » assert_select plugin for Rails

      [...] Update: The new release of assert_select includes support for CSS pseudo classes (nth-child, first-child, empty). More details here. It also supports nested assertions for dealing with lists, tables and forms. Some examples here. I updated this post to use nested asserts. [...]

    2. Jul 31st, 2006

      Kevin Clark

      This is much better.. how does the ? substitution work exactly?

    3. Jul 31st, 2006

      Assaf

      Kevin,

      Just like ActiveRecord conditions, you use question mark (?) to substitute and then pass a value in the assert_select arguments list. Values are read in the order in which they appear in the selector.

      You can use substitution for ID, class name, attribute value or any of the values uses in pseudo classes.

      For example:

      assert_select “div#? a[href=?]“, /id-\d+/, url_for(:action=>”index”)

      It will match the ID attribute of each div based on the pattern id-nnnn, and will match the links that point to the URL for the action “index”.

    Your comment, here ⇓

    Or using OpenID