public
Description: View testing that doesn't suck.
Homepage:
Clone URL: git://github.com/nakajima/elementor.git
name age message
file README.textile Tue Nov 25 07:38:09 -0800 2008 added todo for chaining of filtered results [nakajima]
file elementor.gemspec Thu Jan 15 13:45:26 -0800 2009 bumped version to 0.0.8 [Mike Dalessio]
directory lib/ Sat Mar 14 07:48:42 -0700 2009 Allow proc with :from option [leshill]
directory spec/ Wed May 13 14:17:04 -0700 2009 Fixed test failures from Nokogiri upgrade [nakajima]
README.textile

Elementor

Prettier element traversal with Nokogiri.

Elementor lets you alias Nokogiri CSS searches with method
names, returning an extended Nokogiri document ,upon which you
can call these alias methods.

To use it, include the Elementor module, then call elements,
pass it options then a block in which you’ll specify your element
names. The only option you have to specify currently is :from,
which is the method that will be called to return a raw markup
string to be parsed by Nokogiri.

Usage

require 'rubygems'
require 'elementor'

include Elementor

def body
  <<-HTML
  <h1>This is the header</h1>
  <div id="detail-section">
    <span>This is a detail</span>
    <span>So is this</span>
    <span>Oh one more!</span>
  </div>
  HTML
end

doc = elements(:from => :body) do |tag|
  tag.headers "h1"
  tag.details "#detail-section span"
end

# The standard
p doc.headers # => ["This is the header"]
p doc.details # => ["This is a detail", "So is this", "Oh one more!"]

# Using the `with_text` filter
p doc.details.with_text("This is a detail") # => ["This is a detail"]

Useful Usage

(separate from above example)

require 'elementor'
require 'elementor/spec'
  
# I don't like testing views at this level from the
# controller spec, but this is just an example. I'd
# recommend using Elementor with whatever view test
# setup you prefer.
describe FoosController do
  include Elementor

  describe "#index" do
    before(:each) do
      @result = elements(:from => :body) do |doc|
        doc.tags "ul#tag-cloud li"
        doc.ajax_forms "form.ajaxified"
        doc.user_links "ul#users li a"
      end
    end

    it "renders tag cloud tags" do
      @result.should have(52).tags
    end
    
    it "renders ajax forms" do
      @result.should have(3).ajax_forms
    end

    it "renders user links" do
      @result.should have(6).user_links
    end
    
    # this one uses the `with_text` filter
    it "renders user link for Pat" do
      @result.should have(1).user_links.with_text("Pat")
    end
    
    # this one uses the `with_attrs` filter
    it "renders active user links" do
      @result.should have(1).user_links.with_attrs(:class => /active/)
    end
  end
end

View the CI build.

Todo

  • Allow chaining of filter results
  • Maybe a better way of getting raw markup strings?