Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
[ANN] assert_xhtml - test your HTML by example
Phlip
3/12/2009 4:58:00 AM
Rubies:
My quest to get the maximum coverage & diagnostics from the leanest possible
tests has reached a new level.
This is a Rails functional test on an HTML form:
user = users(:Moses)
get :edit_user, :id => user.id
assert_xhtml do
form :action => '/users' do
fieldset do
legend 'Personal Information'
label 'First name'
input :type => 'text',
:name => 'user[first_name]'
:value => user.first_name
end
end
end
That's all. The assertion expects a form with a given action, containing a
fieldset, a legend, a label, and a populated text input field. The assertion
forgives any other details, such as intervening structural tags, and complains
if any required detail is missing, out of order, or ill-formed.
You test HTML by writing an example of what you expect, excluding details you
don't care about.
When it fails, the assertion will print out your reference HTML, and your sample
HTML from your web page.
The assertion faults if...
- any attribute does not match exactly
- any text content does not match, stripped
- any node is not found at all
- any node is found out of order
The assertion does not fail if
- nodes contain un-specified attributes
- the DOM contains any un-specified tags (<html>, <div>, etc.)
Here's the assertion checking that a list appears in collating order:
assert_xhtml SAMPLE_LIST do
ul :style => 'font-size: 18' do
li 'model' do
li 'Billings criteria'
li 'Billings report'
li 'Sales report'
end
end
end
The assertion takes a block that renders into a Nokogiri::HTML::Builder. Any
HTML it can build, you can specify by example.
That freedom causes two issues: The 'self' context is different inside this
block, so you must pass variables in as closures. And an element with the same
name as a Builder method, such as .select, need a bang: .select!
Get the assertion, from assert2-0.3.8.gem, with these incantations:
gem install assert2 nokogiri
require 'assert2/xhtml' # for Test::Unit::TestCase and derivatives
Those of you using RSpec will know how to apply this Gist:
http://gist.github...
Good hunting!
--
Phlip
http://www.zerop...
3 Answers
David Chelimsky
3/12/2009 8:36:00 PM
0
On Wed, Mar 11, 2009 at 11:58 PM, Phlip <phlip2005@gmail.com> wrote:
> Rubies:
>
> My quest to get the maximum coverage & diagnostics from the leanest possi=
ble
> tests has reached a new level.
>
> This is a Rails functional test on an HTML form:
>
> =A0 =A0user =3D users(:Moses)
> =A0 =A0get :edit_user, :id =3D> user.id
>
> =A0 =A0assert_xhtml do
>
> =A0 =A0 =A0form :action =3D> '/users' do
> =A0 =A0 =A0 =A0fieldset do
> =A0 =A0 =A0 =A0 =A0legend 'Personal Information'
> =A0 =A0 =A0 =A0 =A0label 'First name'
> =A0 =A0 =A0 =A0 =A0input :type =3D> 'text',
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0:name =3D> 'user[first_name]'
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0:value =3D> user.first_name
> =A0 =A0 =A0 =A0end
> =A0 =A0 =A0end
>
> =A0 =A0end
>
> That's all. The assertion expects a form with a given action, containing =
a
> fieldset, a legend, a label, and a populated text input field. The assert=
ion
> forgives any other details, such as intervening structural tags, and
> complains if any required detail is missing, out of order, or ill-formed.
>
> You test HTML by writing an example of what you expect, excluding details
> you don't care about.
>
> When it fails, the assertion will print out your reference HTML, and your
> sample HTML from your web page.
>
> The assertion faults if...
>
> =A0- any attribute does not match exactly
> =A0- any text content does not match, stripped
> =A0- any node is not found at all
> =A0- any node is found out of order
>
> The assertion does not fail if
>
> =A0- nodes contain un-specified attributes
> =A0- the DOM contains any un-specified tags (<html>, <div>, etc.)
>
> Here's the assertion checking that a list appears in collating order:
>
> =A0 =A0assert_xhtml SAMPLE_LIST do
>
> =A0 =A0 =A0ul :style =3D> 'font-size: 18' do
> =A0 =A0 =A0 =A0li 'model' do
> =A0 =A0 =A0 =A0 =A0li 'Billings criteria'
> =A0 =A0 =A0 =A0 =A0li 'Billings report'
> =A0 =A0 =A0 =A0 =A0li 'Sales report'
> =A0 =A0 =A0 =A0end
> =A0 =A0 =A0end
>
> =A0 =A0end
>
> The assertion takes a block that renders into a Nokogiri::HTML::Builder. =
Any
> HTML it can build, you can specify by example.
>
> That freedom causes two issues: The 'self' context is different inside th=
is
> block, so you must pass variables in as closures. And an element with the
> same name as a Builder method, such as .select, need a bang: .select!
>
> Get the assertion, from assert2-0.3.8.gem, with these incantations:
>
> =A0gem install assert2 nokogiri
>
> =A0require 'assert2/xhtml' =A0# =A0for Test::Unit::TestCase and derivativ=
es
>
> Those of you using RSpec will know how to apply this Gist:
>
> =A0
http://gist.github...
Any interest in just incorporating this into the lib? So an RSpec user
could say "require 'assert2/rspec'" or something?
>
> Good hunting!
>
> --
> =A0Phlip
> =A0
http://www.zerop...
>
>
Phlip
3/12/2009 10:26:00 PM
0
>> Those of you using RSpec will know how to apply this Gist:
>>
>>
http://gist.github...
>
> Any interest in just incorporating this into the lib? So an RSpec user
> could say "require 'assert2/rspec'" or something?
Yabsolutely!
And my boss just hit the "crunch mode" button today, after 2 years of pure
sustainable pace. Guess it's time to pay out the energy we have stored up, huh?
(No RSpec onsite. Sorry!)
Marcin Raczkowski
3/13/2009 9:09:00 PM
0
Amazing stuff :D
Can't wait to test it out
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
[ANN] assert_xhtml - test your HTML by example
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password