[rspec-users] Phlip's be_html_with was RE: Problem with Custom matcher and Blocks

Phlip phlip2005 at gmail.com
Sat Apr 4 08:40:07 EDT 2009

Brandon Olivares wrote:

> I just found a custom matcher created by Phlip at
> http://gist.github.com/76136

That's just a sketch. The real deal is at...

   gem install nokogiri assert2

   require 'assert2/xhtml'

Report if that works better. You might find its inside source code is a little 
nicer, too.

> So I added that to my project, and it mostly works great for what I'm trying
> to do, and the syntax is pretty nice.
> But anytime I try to use select, I get an error. I wonder if it's
> conflicting with another method somewhere else?


Use select!. That's a missing feature in Nokogiri::HTML::Builder, and its author 
might fix it. Until then, Nokogiri and I use bangs for three reasons:

   convert a misunderstanding into innocent HTML
   convert potential HTML into a new keyword (:xpath!, without!)
   convert an element.class shortcut into an element.id!

> So I wondered if there is a way to get around this, since I'd like to test
> that my select field is there.

       it "should have a subject dropdown box" do
           response.body.should be_html_with {
             form.contact! do
               label 'Subject'
               select! :id => 'subject'
       end # it "should have a subject dropdown box"

Notice two things, folks - Brandon started his assertion with a unique container 
object. That's nearly the only way to get reasonable diagnostics. I gave up on 
trying to report the "closest match", and now I only report the "first extent of 
HTML that matches your first element".

Also notice that all assertions should have a "diagostic message" facility - 
like the third argument to the lowly assert_equal(in, out, message = nil). But 
assert_xhtml does not have a message=nil yet.

I'm too busy this weekend making it interpret Ajax, as a drop-in replacement for 

     assert_rjs :replace_html, :label_7 do
       input.Top_Ranking! :type => :checked, :value => :Y
       input.cross_sale_1, :type => :hidden, :value => 7

You write the RJS you need to match (like assert_rjs classic uses), and then you 
add an assert_xhtml block, and it works on the Element.update() payload itself.



More information about the rspec-users mailing list