Lisa Crispin wrote:
> We definitely want to do these investigative efforts. But I still feel
> examples are a good way to start. What have other folks found on their
> projects?
I have to confess a low rate of adoption for my assert_xpath project, to
unit-test raw HTML. As a computer scientist and a DSL-zealot, I _like_ writing
inhumanly elaborate queries, like '//fieldset[ following-sibling::input[ @name =
"etc" ] ]'.
Someone on the net recently challenged a forum to write an assertion like that,
but in this format, and I did it:
assert_xhtml do
form @action do
fieldset do
label 'first name'
input :name => 'etc'
end
end
end
Easier to read, huh? The HTML element names are all raw Ruby; they call
method_missing(), to convert into XPath expressions like that, on the fly.
That forum was the RSpec mailing list, and this leads to an interesting
observation. RSpec, and Behavior Driven Development, were _supposed_ to be about
the customer tests. Yet these days the general Ruby on Rails community now
considers TDD (and the test/unit library) to be OLD SCHOOL!
All the new, cool projects use RSpec. It appears that programmers like to
specify their designs by example, too.
Here's that assertion's RSpec version:
@response.body.should be_html_with{
form @action do
fieldset do
label 'first name'
input :name => 'etc'
end
end
}
Like all good examples, those assertions skip over irrelevant details, such as
intervening <table>, <div>, <li> tags, or whatever.
(Those of you with Ruby can try those by grabbing the assert2 gem; then calling
require 'assert2/xhtml'.)
--
Phlip
http://www.zeroplayer.com/