Phlip's be_html_with was RE: Problem with Custom matcher and Blocks


#1

Hi,

I just found a custom matcher created by Phlip at

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?

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 :id => 'contact' do
          label 'Subject'
          select :id => 'subject'
        end
    }
  end # it "should have a subject dropdown box"

Thanks,
Brandon


#2

Uh oh.

          select! :id => 'subject'

select!.subject!

!


#3

Brandon O. 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?

http://groups.google.com/group/merb/browse_thread/thread/3588d3f75fa0e65c

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
      }
   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:

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

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.

http://groups.google.com/group/ruby-talk-google/msg/b94d83ad2d5e6536


Phlip
http://www.zeroplayer.com/


#4

Phlip,

Well, now it’s not failing if there is an error. This passes:

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

As it should, but the following passes as well, which should not:

  it "should have a name field" do
    response.body.should be_html_with {
      form.contact! do
        label 'Name'
        input.name!
      end
    }
  end # it "should have a name field"

There is most definitely not a name field, nor its associated label.

Thanks,
Brandon


#5

Brandon O. wrote:

  it "should have a name field" do
    response.body.should be_html_with {
      form.contact! do
        label 'Name'
        input.name!
      end
    }
  end # it "should have a name field"

There is most definitely not a name field, nor its associated label.

Can you try the assert_xhtml version? I think it will drop in with:

Spec::Runner.configure do |c|
c.include Test::Unit::Assertions
end

be_html_with{} is something of a nail-soup issue for me because we
always only
use test/unit at my day job… sorry!

I will look at the issue this weekend - the @failure_message might not
bubble up
to the RSpec wrapper correctly…


#6

Hi,

Yes that’s great! That works perfectly.

I’d appreciate it if you could let me know about the be_html_with issue
when
you can though, as I like the natural language better.

Thank you very much.

Brandon


#7

Phlip,

Wow thank you very much.

Sorry for the double post, I originally accidently sent my post from
another
email address.

Anyway, that’ll be a lot better, and more refined than my previous
syntax.

Also I really like your RJS assertions. I’ll have to test that out when
I
get to using AJAX.

Thanks,
Brandon


#8

Brandon O. wrote:

I’d appreciate it if you could let me know about the be_html_with issue when
you can though, as I like the natural language better.

Add this monkey patch below require ‘assert2/xhtml’:

class BeHtmlWith
def matches?(stwing, &block)
@block ||= block
@scope.wrap_expectation self do
@doc = Nokogiri::HTML(stwing)
return run_all_xpaths(build_xpaths)
end
end
end

Now, I’m off to finish manual tests of the new assert_rjs, and to pop a
new
version…


#9

Hi,

Thank you very much.

Is there any documentation of the rest of assert2? I found a web site I
think (assuming it’s the same thing), but there wasn’t really
documentation
for assert_xhtml or assert_rjs.

Brandon


#10

Brandon O. wrote:

Yes that’s great! That works perfectly.

I’d appreciate it if you could let me know about the be_html_with issue when
you can though, as I like the natural language better.

i dunno about “natural language”, but I expect to fix the RSpec language
this
weekend. (-;


#11

Brandon O. wrote:

Is there any documentation of the rest of assert2? I found a web site I
think (assuming it’s the same thing), but there wasn’t really documentation
for assert_xhtml or assert_rjs.

assert_rjs is still in the oven, and assert_xhtml arrived too fast to
get
anything more than its README:

http://assert2.rubyforge.org/svn/README

I used to take a train to work - that’s why the assert{ 2.0 }
documentation
itself is so… florid…