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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Brandon O. (Guest)
on 2009-04-04 11:14
(Received via mailing list)
Hi,

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

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
Phlip (Guest)
on 2009-04-04 17:19
(Received via mailing list)
Uh oh.

>               select! :id => 'subject'

   select!.subject!

!
Phlip (Guest)
on 2009-04-04 17:38
(Received via mailing list)
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/...

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/ms...

--
   Phlip
   http://www.zeroplayer.com/
Brandon O. (Guest)
on 2009-04-04 18:45
(Received via mailing list)
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
Brandon O. (Guest)
on 2009-04-04 20:53
(Received via mailing list)
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
Phlip (Guest)
on 2009-04-04 22:01
(Received via mailing list)
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...
Brandon O. (Guest)
on 2009-04-05 00:47
(Received via mailing list)
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
Phlip (Guest)
on 2009-04-05 07:02
(Received via mailing list)
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. (-;
Phlip (Guest)
on 2009-04-05 09:16
(Received via mailing list)
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...
Brandon O. (Guest)
on 2009-04-05 12:31
(Received via mailing list)
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
Phlip (Guest)
on 2009-04-05 15:18
(Received via mailing list)
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...
This topic is locked and can not be replied to.