Forum: Ruby assert2/xhtml specifies your HTML by example

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Phlip (Guest)
on 2009-03-16 05:46
(Received via mailing list)

assert_xhtml now works with RSpec. (Call it ".should be_html_with{}" !)

This is its Rails functional test aspect:

     get :edit_user, :id => users(:Moses).id
     scope = self

     assert_xhtml do
       form :action => '/users' do
         fieldset do
           legend 'Personal Information'
           label 'First name'
           input :type => 'text',
                 :name => 'user[first_name]'
                 :value => scope.users(:Moses).first_name

The assertion tests your form "by example". Your page must contain a
form with
the given action, and it must contain at least the listed elements,
and text. Anything you don't care about - anything your designers might
change -
you simply leave out of the example.

(The example is actually Nokogiri::HTML::Builder notation, so anything
can do, the assertion can do. But that's why we needed that 'scope'
variable, as
a kind of "messenger rope", to call your test-side methods inside the
Otherwise, Nokogiri would dutifully convert them into HTML!)

Here's a similar test, in RSpec:

   it 'should have a cute form' do
     render '/users/new'

     response.body.should be_html_with{
       form :action => '/users' do
         fieldset do
           legend 'Personal Information'
           label 'First nome'
           input :type => 'text', :name => 'user[first_name]'

Oh, except it has a bug in it! Here's the diagnostic:

'/users/new should have a cute form' FAILED

Could not find this reference...

<form action="/users">
   <legend>Personal Information</legend>
   <label>First nome</label>
   <input type="text" name="user[first_name]">
</fieldset></form> these sample(s)...

<form action="/users">
     <legend>Personal Information</legend>
       <li id="control_user_first_name">
         <label for="user_first_name">First name</label>
         <input type="text" name="user[first_name]"

Notice, as usual, the specification diagnostic only contained the start
of the
match. The example with <form action="/users"> worked.

That's all there is to it. Write anything you like inside the block, and
if the
assertion can't find it, it will explain why it can't. Install the gem
like this:

   gem install nokogiri assert2  #  make sure the later is 0.3.9!

Then use require 'assert2/xhtml', in either the test/unit or RSpec

This project is wide-open for suggestions - with < 200 lines of code
plenty of room for more features!
This topic is locked and can not be replied to.